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

Started by
9 comments, last by GameDev.net 19 years, 3 months ago
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! :)
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.
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. ^^,
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
You'd almost never want to catch ALL exceptions at once.

Using
catch(Exception) {}// orcatch {}

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
Andre Loker | Personal blog on .NET
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

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
What the hells!
http://msdn.microsoft.com/msdnmag/issues/04/06/NET/

Good article on best practices related to exceptions in .NET.
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.

This topic is closed to new replies.

Advertisement