A Java exception is an object that describes an exception (that is, an error) that occurs in a code snippet. When an exception occurs, an object representing the exception is created and thrown (throw) in the method that caused the error. The method can choose to handle the exception itself or pass the exception. In both cases, the exception is captured (caught) and processed. Exceptions may be generated by the Java runtime System, or by your manual code. Exceptions thrown by Java are related to basic errors that violate the language specification or exceed the Java execution Environment limit. The exception that is generated by hand coding is basically used to report the error condition of the method calling program.
Java exception handling is controlled by 5 keywords: try, catch, throw, throws, and finally. Here's how they work. The program declares that the exception monitor you want is included in a try block. If an exception occurs in the try block, it is thrown. Your code can catch the exception (with catch) and handle the exception in some reasonable way. System-generated exceptions are automatically thrown by the Java runtime System. Throw an exception manually, throw with the keyword. Any exception to the thrown method must be defined by the throws clause. Any code that is absolutely executed before the method returns is placed in the finally block.
The following is the usual form of an exception handling block:
1 Try {2 //block of code to monitor for errors3 }4 Catch(ExceptionType1 exOb) {5 //exception handler for ExceptionType16 }7 Catch(ExceptionType2 exOb) {8 //exception handler for ExceptionType29 }Ten // ... One finally { A //block of code to be executed before try block ends -}
Here, Exceptiontype is the type of exception that occurs.
Series Articles:
Java know how much (top)
Java know how much (interface) interface
Java knows how much (40) the difference between an interface and an abstract class
Java know how much (41) generic explanation
Java know how much (42) the range of generic wildcard characters and type parameters
Java know how much (43) Exception Handling Basics
Java know how much (43) Exception Handling Basics