Previous Page
Next Page

Chapter 6 Quick Reference

To

Do this

Throw an exception

Use a throw statement. For example:

throw new FormatException(source);

Ensure that integer arithmetic is always checked for overflow

Use the checked keyword. For example:

int number = Int32.MaxValue; 
checked 
{ 
    number++; 
}

Catch a specific exception

Write a catch handler that catches the specific exception class. For example:

try 
{ 
    ... 
} 
catch (FormatException fEx) 
{ 
    ... 
}

Catch all exceptions in a single catch handler

Write a catch handler that catches Exception. For example:

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

Ensure that some code will always be run, even if an exception is thrown

Write the code inside a finally block. For example:

try 
{ 
    ... 
} 
finally 
{ 
    // always run 
}

Previous Page
Next Page