Mega Code Archive

 
Categories / Java Book / 001 Language Basics
 

0098 Exception Handling

An exception is a run-time error. Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. Program statements that might have exceptions are contained within a try block. The exception handler is coded using catch statement To manually throw an exception, use the keyword throw. Any exception that is thrown out of a method must be specified by a throws clause. Any code that would be executed regardless after a try block is put in a finally block. This is the general form of an exception-handling block: try { // block of code to monitor for errors } catch (ExceptionType1 exOb) { // exception handler for ExceptionType1 } catch (ExceptionType2 exOb) { // exception handler for ExceptionType2 } // ... finally { // block of code to be executed after try block ends } Here, ExceptionType is the type of exception that has occurred