Exceptions: What and Why?
Home ] Up ] [ Exceptions: What and Why? ] Method Signatures and Exceptions ] Classification of Exceptions ] Catching Exceptions ] Throwing an Exception ] Exception Hierarchies ]

 

 

What's an Exception?

  • Exceptions are "exceptional events"
  • Events that are outside the normal flow of a program
  • An exception is a subclass of class Exception (actually, Throwable, but more on that later)
  • An exception may be "thrown" automatically, or by user code
  • Can be "caught" by user code

Why Should I Care?

  • Do you check for errors when you make function calls?
    • If not, you should!
    • If you do, you probably have found that the amount of code that you have to write for error checking is often more than the main flow of the code.
  • Exceptions allow you to write the main flow of the code more simply, and separate out the nitty gritty details of what to do when something goes wrong.
  • Exceptions also allow you to choose where to place the code to handle particular exceptions:
    • Exceptions that must be handled in the current procedure can be
    • However, some exceptions should be handled by procedures higher up the call stack.
  • Exceptions allow you to clean up your code to a considerable degree
  • Exceptions also force you to think about the exceptions that can occur, and have to deal with them.
 
The page was last updated February 19, 2008