Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Try,Catch,Throw,Finally...C#  (Read 2466 times)

0 Members and 1 Guest are viewing this topic.
Try,Catch,Throw,Finally...C#
« on: September 29, 2011, 03:24:34 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
Can someone give me a perspective on this from a game programming point of view?  I been reading my C# book and for some reason I can't wrap my head around it.  Like, what is useful about Try, Catch, Throw, and Finally in C#?  Just having trouble putting it together or finding it useful.
Logged
  • Super Fan Gamers!
Re: Try,Catch,Throw,Finally...C#
« Reply #1 on: September 29, 2011, 03:37:21 am »
  • Minalien
  • *
  • Reputation: +10/-1
  • Offline Offline
  • Gender: Female
  • Posts: 2119
Would you rather your program crash, or handle runtime errors gracefully? Exceptions allow you to handle runtime errors that occur, for whatever reasons.

Try
A try block is the code that could possibly throw an exception. This will be code that could possibly fail with poor user input, or if the user decided to move files around.

Catch
The code in this block will be called when an exception of the appropriate type is thrown. This is where you handle the issue - whether by substituting a "missing texture" placeholder or gracefully informing your player that they messed it up, then closing.

Finally
This will be called only if the code in the try block does not fail. Use this to clean up any resources, or perform code that relies on the success of the code in the try block.

Throw
This is how you throw exceptions. If you're writing an XML-based settings file reader and it runs into an unknown element (say, <GiveMeAllTehCheatz Name="T3hCheatahz" />), you can use this to notify the code that's telling your reader to open this file, without returning some obscure error code that nobody cares about.
Logged
Quote
There's such a double standard about religion in the modern world. Catholics can gather, wear white robes, and say "In nomine Patris, et Filii, et Spiritus Sancti" and be considered normal.

But if my friends and I gather, wear black robes, and say  "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn", we're considered cultists.
  • Development Blog
Re: Try,Catch,Throw,Finally...C#
« Reply #2 on: September 29, 2011, 09:59:01 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 2245
Finally
This will be called only if the code in the try block does not fail. Use this to clean up any resources, or perform code that relies on the success of the code in the try block.
Incorrect, it will always be the last called when you exit the try block. Keep in mind this also includes returning a value, continue; break; etc.

It also allows you to create more stable code since you can eliminate cases which you don't want to deal with and ensure that the classes you've written are used in the way you indented.
Logged
Re: Try,Catch,Throw,Finally...C#
« Reply #3 on: September 29, 2011, 01:57:12 pm »
  • Minalien
  • *
  • Reputation: +10/-1
  • Offline Offline
  • Gender: Female
  • Posts: 2119
Oh. D:

Well, that one's lacking in C++, so I've used it all of zero times. :s
Logged
Quote
There's such a double standard about religion in the modern world. Catholics can gather, wear white robes, and say "In nomine Patris, et Filii, et Spiritus Sancti" and be considered normal.

But if my friends and I gather, wear black robes, and say  "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn", we're considered cultists.
  • Development Blog
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.063 seconds with 46 queries.