Jump to content
  • Advertisement
Sign in to follow this  
Riddle

[.net] Exceptions, catch all in one or not?

This topic is 4973 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all! :) I have a question regarding exceptions.(C#) What is better, every exception listed:
       try
       {
           FileStream file = File.OpenRead( this.folderName + Path.DirectorySeparatorChar + fileName );
       }
       catch( DirectoryNotFoundException )
       {
           throw;
       }
       catch( FileNotFoundException )
       {
           throw;
       }
      // ...


or:
       try
       {
           FileStream file = File.OpenRead( this.folderName + Path.DirectorySeparatorChar + fileName );
       }
       catch
       {
           throw;
       } 
      // ...


IN such cases where I want to catch the most important. ( Null reference exception too?hmm.. ) Thank you! :)

Share this post


Link to post
Share on other sites
Advertisement
If you handle two exceptions types differently you should catch them seperately. If you handle all exception types the same way you should catch them all together. If you aren't handling the exception at all, you shouldn't have a try/catch block in the first place.

Share this post


Link to post
Share on other sites
Well, I don't want to let it crash because of an exception.

OpenRead may throw:

/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="FileNotFoundException"></exception>
/// <exception cref="UnauthorizedAccessException"></exception>
/// <exception cref="NotSupportedException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="NotSupportedException"> If the path has an invalid format. </exception>
/// <exception cref="ArgumentException"> If the fileName/folder contains invalid arguments/characters. </exception>
/// <exception cref="ArgumentNullException"> If the fileName/folder is null. </exception>





I wonder why engines like Axiom don't catch exeptions at all.. ( last build I saw )




Thank you for your help SiCrane. ^^,

Share this post


Link to post
Share on other sites
Quote:
Original post by Riddle
Well, I don't want to let it crash because of an exception.


The rule is to handle exceptions at the point in the code where you can do something about the problem that caused the exception. Which may not give you much of a hint, I'll grant you that much.

Quote:

I wonder why engines like Axiom don't catch exeptions at all.. ( last build I saw )


It could be because many programmers don't know how to use exceptions in the first place; or because some of those exceptions they throw are better handled by client code you write.

Share this post


Link to post
Share on other sites
Quote:
Original post by Riddle
Well, I don't want to let it crash because of an exception.

You do realize that code you have doesn't contribute to that goal at all, right? Again, if all your catch block does is rethrow the exception, you might as well not have a catch block in the first place.

Share this post


Link to post
Share on other sites
You'd almost never want to catch ALL exceptions at once.

Using

catch(Exception) {}
// or
catch {}

to handle several exceptions at once is EVIL. Always keep in mind that exceptions like ExecutionEngineException might be thrown somewhere. Simply ignoring the severity of those exceptions is deadly.

Regards,
Andre

Share this post


Link to post
Share on other sites
Well i have catches that catch everything, throughout my engine. I have written a class that handles all exceptions for me, so i simply rethrow the exception, then catch it again inside the calss when it is called.

Works well.

ace

Share this post


Link to post
Share on other sites

try
{
FileStream file = File.OpenRead( this.folderName + Path.DirectorySeparatorChar + fileName );
}
catch( DirectoryNotFoundException )
{
throw;
}
catch( FileNotFoundException )
{
throw;
}


That´s plainly stupid, if you don´t do anything at the catch block, why catch it and rethrow it instead of letting it unwind alone?, you get exactly the same without polluting your code with 11 extra lines over 1 line

PS: At least you use "throw" instead of "throw e", which starts a new StackTrace ;)

PSS: On topic: catch each concrete exception when you can do somethiung about it, catch all exceptions around the main loop to show a "nice" message to the user and die elegantly

Share this post


Link to post
Share on other sites
http://msdn.microsoft.com/msdnmag/issues/04/06/NET/

Good article on best practices related to exceptions in .NET.

Share this post


Link to post
Share on other sites
In my main function I usually put a try/catch block that can catch any type of exception, and it assumes that these exceptions are fatal. If anywhere I might encounter a non-fatal exception or an exception that needs to be handles differently, I simply add another try/catch block there.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!