4

What is right way to do.

To catch exceptions from most specific to most general or opposite.

if I write

try { ... } catch( Exception e ) { ... } catch( NullReferenceException nre ) { ... } 

Will NullReferenceException nre ever be caught?

5
  • 19
    Easily tested, surely Commented Oct 18, 2010 at 14:43
  • Some more tips regarding exceptions in general. Best Practices for Handling Exceptions Commented Oct 18, 2010 at 14:53
  • Is it really easy to test something will ever occur? Commented Oct 22, 2010 at 21:51
  • 1
    Yes, just put a throw new NullReferenceException() in the try block and if that catch statement isn't called then there are no other cases to test. Commented Feb 14, 2013 at 15:56
  • that wont even compile. Commented Jun 2, 2016 at 9:43

9 Answers 9

14
try { ... } catch( NullReferenceException nre ) { ... } catch( Exception e ) { ... } 

Also I wouldn't be catching NullReferenceException, I would test if the value I am trying to access is not null before actually accessing it or use the null coalescing operator (??) to make sure this exception never happens.

Catching a general Exception should be avoided. IMHO you should do this only in some global exception handler because you can rarely handle the case of all possible exceptions every time you call a method. In this case you should only catch some specific exceptions if any and take proper actions.

Sign up to request clarification or add additional context in comments.

Comments

2

No, you have to go from the most specific to the most general. Your example has to look like

try { } catch(NullReferenceException nre) { } catch(Exception e) { } 

see here (MSDN)

Comments

1

No it won't.

It goes in the order you place it. Put the most specific exceptions at the top and the general at the bottom.

http://msdn.microsoft.com/en-us/library/0yd65esw.aspx

Comments

1

No. You should catch exceptions from the most specific to general.

Comments

1

most derived to less derived

Comments

1

A try block can throw multiple exceptions, which can handle by using multiple catch blocks. Remember that more specialized catch block should come before a generalized one. Otherwise the compiler will show a compilation error.

Source

Comments

0

Most specific first

Will NullReferenceException nre ever be caught?

true

no, it won't be caught

4 Comments

Are you sure? How can NRE be caught in this example?
it is not true, you can check
i mean that is never be caught, is it what you mean?
This is actually incorrect; you'll be unable to compile the code.
0

In order to be caught, the NullReferenceException shall be the first into the catch-list.

try { ... } catch (NullReferenceException ex) { .... } catch (Exception ex) { ... } 

This specifies that you wish to handle the NullReferenceException in a particular way, that you have something specific to do with. Then, letting the other types of exceptions fall through the most generic catch. On the other hand, non-specific exception handling should be avoided as this MSDN article suggests: Exception Handling.

Besides, it is better to verify whether the object to be accessed is null (Nothing in Visual Basic) before trying to access it, instead of letting the code access the object and throw this exception when it is null. Here's a useful link for this matter: Exception Handling Best Practices in .NET.

Comments

0

of course its from most specific to general.

try { ... } catch (IOException ex) { .... } catch (Exception ex) { ... } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.