Difference between revisions of "Exceptions"
From Suhrid.net Wiki
Jump to navigationJump to searchLine 1: | Line 1: | ||
+ | == Introduction == | ||
* Exceptions are a mechanism to detect and handle errors without writing special code to test return values. | * Exceptions are a mechanism to detect and handle errors without writing special code to test return values. | ||
* Error handling code for common errors can be handled in a single place. | * Error handling code for common errors can be handled in a single place. | ||
+ | |||
+ | == Structure == | ||
+ | |||
* A try clause has to have either a catch clause or a finally clause or both. | * A try clause has to have either a catch clause or a finally clause or both. | ||
* catch must follow try. | * catch must follow try. | ||
* finally must follow the last catch clause. | * finally must follow the last catch clause. | ||
+ | |||
+ | == Exception Hierarchy == | ||
+ | |||
+ | * All exception classes are subtypes of java.lang.Exception. | ||
+ | * RunTimeException is a subtype of Exception which represents unchecked exceptions. All unchecked exception will inherit from this class. | ||
+ | * Classes which derive from java.lang.Error indicate serious problems that an application should not try to catch. e.g OutOfMemory error. | ||
+ | * Both Error and Exception classes are derived from the java.lang.Throwable class. | ||
+ | |||
+ | == Exception Matching == | ||
+ | |||
+ | |||
[[Category:OCPJP]] | [[Category:OCPJP]] |
Revision as of 23:01, 19 July 2011
Introduction
- Exceptions are a mechanism to detect and handle errors without writing special code to test return values.
- Error handling code for common errors can be handled in a single place.
Structure
- A try clause has to have either a catch clause or a finally clause or both.
- catch must follow try.
- finally must follow the last catch clause.
Exception Hierarchy
- All exception classes are subtypes of java.lang.Exception.
- RunTimeException is a subtype of Exception which represents unchecked exceptions. All unchecked exception will inherit from this class.
- Classes which derive from java.lang.Error indicate serious problems that an application should not try to catch. e.g OutOfMemory error.
- Both Error and Exception classes are derived from the java.lang.Throwable class.