Exception Handling
Exception Handling
Exception
Handling
Basic Terminology
• Code Example
Code Example
Exception Properties
• Example:
– Accounting system might have account-number processing code in which account
numbers are input a strings, but represented with integers in the code.
- Convert.ToInt32 can throw a FormatException when it encounters an invalid number format.
- When an invalid account-number format occurs, the accounting system code might need to
indicate an error message different from the one supplied by FormatException or to indicate
a new exception such as InvalidAccountNumberFormatException.
- In this case, the program supplies code to catch the FormatException, then create an
exception object in the catch handler, passing original execution as one of the constructor
arguments.
- The original exception object becomes the InnerException of the new exception object.
- Code Example
Exception Properties
Programmer-Defined Exceptions