Java Exception handling
Exception Handling ?
Exceptions are generated when a recognized an error condition during the execution of a program. Java includes a system for running exceptions, by tracking the potential for each method to throw specific exceptions.
An exception is an event, which occurs during the execution of the proram, that an interrupt the normal flow of the program's instruction. In other words, Exceptions are generated when a recognized condition, usually an error condition, arises during the execution of a method.
Java includes a system for running exceptions, by tracking the potential for each method to throw specific exceptions. For each method that could throw an exception, your code must report to the Java compiler that could throw that exact exception. The compiler marks that method as potentially throwing that exception, and then need any code calling the method to handle the possible exception.
An exception is an event, which occurs during the execution of the program, that an interrupt the normal flow of the program's ins
Errors and its types ?
An error in a program is called bug. Removing errors from program is called debugging . There are basically three types of errors in the Java program:
(i) Compile time errors:
Errors which occurs due to syntax or format is called compile time errors. These errors are detected by java compiler at compilation time. Desk checking is solution for compile-time errors.
(ii) Runtime errors:
These are the errors that represent computer inefficiency. Insufficient memory to store data or inability of the microprocessor to execute some statement is examples to runtime errors. Runtime errors are detected by JVM at runtime.
(iii) Logical errors:
These are the errors that occur due to bad logic in the program. These errors are rectified by comparing the outputs of the program manually.
Exception Handling:
Exception handling refers to java's handling of run time errors. An exception is an object that is generated at run-time to describe a problem encountered during the execution of a program. Exception handling is basically use five keyword as follows:
- try
- catch
- throw
- throws
- finally
The general format of an exception handling is as follows:
Here, the try, catch and finally are keywords. ExceptionType1, to ExceptionTypeN is the built in Exception classes that can catch a specific exception.
Exception Handling Mechanism
The exception handling mechanism performs the following tasks:
A try block can be written and, if problems occur during its execution, control proceeds to the apporopriate catch block.
When a catch block completes execution, control passes to the statement in the finally block.
Using try and catch block
The try statement contains a block of statements. A try block can be written and if problems occur during its excetion, control proceeds to the appropriate catch block.
The general form is:
try{//statement for monitoring the errors }
Catching Exceptions:
Every try block is associated with a corresponding catch block. A catch block must be written after the try block. Each catch block handles the type of exception indicated by its argument. The arument to the catch block is an object of the class Exception.
Simplest form of the catching an exception is as follows:
try{//statement for monitoring the errrs } catch(exceptiontype excepobj) {//exception-handling block. }
The following program includes try block and catch clause which processes the ArithmeticException generated by the division -by -zero error.
Example 1:
Output :
Exception caught in Catch block Outside try-catch clause
Example 2:
Output :
You should not divide a number by zero I'm out of try-catch block in Java.