eomeroff
eomeroff

Reputation: 9915

catching exceptions C#

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?

Upvotes: 4

Views: 1077

Answers (9)

Arrabi
Arrabi

Reputation: 3768

of course its from most specific to general.

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

Upvotes: 0

Arnis Lapsa
Arnis Lapsa

Reputation: 47567

Most specific first

Will NullReferenceException nre ever be caught?

true

no, it won't be caught

Upvotes: 0

Will Marcouiller
Will Marcouiller

Reputation: 24132

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.

Upvotes: 0

user180100
user180100

Reputation:

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

Upvotes: 1

TalentTuner
TalentTuner

Reputation: 17556

most derived to less derived

Upvotes: 1

Dzinx
Dzinx

Reputation: 57784

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

Upvotes: 1

kemiller2002
kemiller2002

Reputation: 115420

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

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

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.

Upvotes: 14

sloth
sloth

Reputation: 101032

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)

Upvotes: 2

Related Questions