开发者

Different ways to try catch in c#

I had this block of code:

try
 {
// DO SOMETHING
 }
catch (Exception e)
 {  
    throw new WebPartPageUserException("YEAH MESSAGE");
}

and the compiler send me a Warning for not using the var e.

Then I change to

try
{
 // DO SOMETHING
}
catch (Exception)
{  
     throw new WebPartPageUserException("YEAH MESSAGE");
}

And it was ok, but finally I learn that

try
{
    // DO SOMETHING
}
   catch 
{  
   throw 开发者_如何学JAVAnew WebPartPageUserException("YEAH MESSAGE");
}

works too.

Are these 3 blocks doing the same? In that case, which is the best practice?


They are doing the same and they are all bad practice, because you catch all exceptions, without even logging the exception details. It will be impossible to know the real cause for your WebPartPageUserException.


It is dependent on what you want. catch alone will catch any kind of Exception. So yes, it's the same as catch (Exception). Sometimes you want to catch special exceptions in a different way. Like a NullReferenceException, so you have to specify that. If you want the exception as a variable, you can say catch (NullReferenceException e) and then say: e.StackTrace() or something similar.

So, in this case, it's the same, but there are ways that it won't be the same.

I agree on the bad practice of @Daniel, but sometimes it's wanted.

Update
If you catch an exception and throw a new one, you could set the catched exception as an innerException of your new one


You should do this:

try
{
    // DO SOMETHING
}
catch (Exception ex)
{  
    throw new WebPartPageUserException("YEAH MESSAGE", ex);
}

Note the use of ex as inner exception argument in the WebPartPageUserException. Not supplying the inner exception means you're leaving out valuable information about the actual error. You should almost always include the inner exception.


Dunno about best practice but personally I stay away from generic "catch all" try catch block... Try to code such that you test for a scenario that would cause an exception and make the code flow as appropriate to deal with the error case.


catch (Exception) only catches Exceptions of Type Exception or inherited from that. catch catches all Exceptions.

Since most (all?) Exceptions are inherited from Exception, #2 and #3 will do the same. For Best Practice, only catch those Exceptions that you can handle gracefully. (You should account for all Exceptions that your try-Block can throw, or at least try to ^^)


If you're not going to use the Exception reference at all, use the last option. The second option, as written, catches only exceptions derived from "Exception" or subclasses. Using this style of exception handling is useful if you want to distinguish between different kinds of exceptions, but aren't going to use the exception reference. Example:

try
{
     DoSomething();
}
catch (NotSupportedException)
{
     Console.Error.WriteLine("The software won't do what you wanted it to");
}
catch (InvalidOperationException)
{
     Console.Error.WriteLine("A possible programming error in the software?");
}

You probably should be using the exception references however, if only to store them in a log so that you can get an insight into why the program is failing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜