|
|
Flow Control and Exception Handling - Exceptions
- Definition:
- An exception is an event that occurs during the execution of a
program that disrupts the normal flow of instructions.
(Sun tutorial: Handling Errors with Exceptions)
- exceptions provide a clean way to check for errors
- they are an explicit part of a methods contract
- exceptions are thrown at runtime if errors occur when a class is loaded or during method execution
- runtime exceptions are objects of the classes java.lang.RuntimeException, java.lang.Error or their subclasses
- runtime exceptions are also called unchecked exceptions
- code may also throw an exception using the throw statement
- these are non-runtime or checked exceptions
- any exceptions you create in your code should extend java.lang.Exception which implements the interface java.lang.Throwable
- you create your own exceptions to add useful data to an error message or, if you are interested in a particular error
- both forms of exceptions (checked and unchecked) may be caught and handled in exception-handling code
- an uncaught exception is caught by a default handler which halts execution and displays an error message
- exception handling is done using the try-catch-finally statment
|