|
|
Or, use your gamerDNA username: (more...)
| ||||||
| |
![]() |
| | LinkBack | Thread Tools | Rate Thread | Display Modes |
| | #1 (permalink) |
| My e-peen is huge Join Date: May 2002 Location: Boiler Up
Posts: 855
| Exception handling Hey guys can anyone point me in the direction of a good resource to learn some proper methods for exception handling. Specifically for OO C# type stuff. I've been programming for a while now, but I've never really got a good grasp on exception handling. Thanks infoz~ |
| | |
| | #2 (permalink) |
| I have to return some video tapes... Join Date: Jul 2002
Posts: 2,345
+7 Internets | I find it pretty simple, I hope my example helps. There really aren't that many great resources dedicated to this stuff because it is such a simple concept when it comes down to it. try { } catch (Exception e) { } catch (...) { } Exception is the default exception class in C#, almost any built in functionality will throw this type of exception. I suggest just playing around with the class and it's contents to get a feel of what kind of stuff it can contain. You typically want to put your try catches around the things you want to halt if there is a problem or to handle an error. An Example would be like: try { DeleteAllDirectories(); } catch ( Exception e) { WriteError(e.Error); return false; } CreateAllDirectories(); So lets say this is some stupid app that deletes all the directories in a folder, and then recreates them all. You don't want to try creating the directories if you couldn't delete them all because that would lead to further issue. So you put the try/catch around the DeleteAllDirectories function to catch the error that happens so the user knows why, as well as preventing the program from attempting to create the directories. Then you have the throw functionality... You can throw any object your heart desires and it will immediately stop what it's doing and the catch has to grab that object. The catches have to match the object you're throwing. An Example... try { Pants(); } catch (string sError) { WriteError(sError); } void Pants () { if (!bPants) throw "There are no pants!"; } In the above example I've created a try catch that catches a string, I'm throwing a string so that catch will grab it and perform the action I requested. If I threw an exception class since it's unhandled by a catch it would crash the app, which is why you always have to catch your exceptions :P catch(...) will blanketly catch any exception, there good if you just don't want something to crash if it fails Any object caught is essentially destroyed, so you lack any information as to why the exception happened.In C++ we commonly wrap try/catches on top of eachother at the various levels of the app, so if we're 3-4 calls deep each layer throws an exception with it's information and we keep adding to the exception object till we reach the top layer. This full exception is a god send for debugging on client machines as we have the line numbers/stack trace of what happened. Dunno hope this helps a bit :P |
| | |
| | #3 (permalink) |
| My e-peen is huge Join Date: May 2002 Location: Boiler Up
Posts: 855
| Thanks man that helps a bunch. Here's an example of something that kind of confused. I have 3 methods 1 method calls 2nd method and 2nd method calls a helper 3rd method. If the exception happens in the 3rd helper method you can wrap a try catch around it and it will be caught, however the code will return to the 2nd method and continue. If I put no try catch around the 3rd the 2nd will catch it and code will continue at the first. Now if I only have try catch around 1st and exception happens on 3rd it wont be caught at the first lol I really hope this wasn't too confusing. I mean if I'm 3 calls deep how can I have ALL execution terminate without having to set some kind of error flag and check that flag? |
| | |
| | #4 (permalink) | |
| I have to return some video tapes... Join Date: Jul 2002
Posts: 2,345
+7 Internets | Quote:
try { stuff that throws exception } catch (object thrown) { Add error to object throw object; <-- throw it a second time, it gets caught in the level above it repeat till you get the level you want to stop at. } That's pretty much what microsoft does with the exception class, so you can see every level/method that the error was involved with like a stack trace to your error. I found this stuff really confusing at first, most places are just like "omg it throw object you catch like baseball" and that's a half page analogy on how it works. Once I started playing with it the power of it became pretty evident and the simplicity awesome ![]() Last edited by Tripamang; 06-24-2008 at 08:07 PM.. | |
| | |
| | #6 (permalink) |
| Math Enthusiast/Badass MC Join Date: Jun 2002 Location: Seattle
Posts: 735
+5 Internets | Another best practice is to catch specific exceptions before general exceptions. Code Analysis tools like FxCop will complain about catching only generic exceptions. For example, if you were working with an Xml document, you should catch Xml-specific exceptions first: try { Do Xml-specific stuff } catch(XmlException xe) { Display Xml-specific error message } catch(Exception e) { Display unexpected error } Kind of nitpicky but that's how MS likes it. Also, try/catches are pretty resource intensive. |
| | |
| | #7 (permalink) | |
| Registered User Join Date: Dec 2002
Posts: 373
| The above posters have covered the basics pretty well, so follow what they say. In regards to WHEN you should use exceptions vs. return codes: Here is an excerpt from the C++ FAQ Lite (it applies to C# too) that offers a really good explanation of the way exceptions should be used in a professional setting: Quote:
| |
| | |
![]() |
|
| Thread Tools | |
| Display Modes | Rate This Thread |
| |