MI0041 | Q6: How do you handle exception in java? Give with an example
Answer:A Java exception is nothing but an object that defines an exceptional condition, or an error that has occurred in a piece of code.
In Java, we can handle this exception by creating an object that represents the exception, and throwing that object into the method that triggered the error. The method may handle the exception by itself or pass on the exception to another method to handle it. In either of the ways, the exception is caught and processed at some point.
Java exceptions may occur in two ways:
· Java run-time system: These exceptions relate to the fundamental errors that violate the rules of the Java programming language or the constraints of the Java execution environment.· Code generated: The code that we write can manually generate exceptions. These are used to report some error condition to the caller of a method.
Exception Types:
All the different types of exceptions are sub classes of the built-in class Throwable.Under ‘throwable’, there are two exception classes, namely, Exception and Error.
· Exception:
o This class specifies the exceptional conditions that your programs should catch. The subclass of this class will contain the custom exception types created by the program.
o Another important subclass of this exception class is RuntimeException. This subclass contains the exceptions that are automatically defined for programs and includes exceptions such as divide by zero and invalid array indexing.
· Error:
o This class defines exceptions that are not expected to be caught under normal circumstances by your program. Java run-time system uses exceptions of the type Error to indicate errors that are related to the run-time environment.
o An example for such an error is, stack overflow.
In Java, exception handling is performed using five keywords, they are: try, catch, throw, throws, and ‘‘Finally’’. The general exception block is as follows:
try{
//block of code to monitor for errors
}
Catch (ExceptionType1 ex1){
//exception handler for ExceptionType1
}
Catch (ExceptionType2 ex2){
//exception handler for ExceptionType2
}
....
Finally {
//block of code to be executed after the try block ends
}
Example:
class HandleException {
public static void main (String args[]) {
int a, b, c;
try {
//monitor a block of code.
a=0;
b=45;
c=b/a;
System.out.println(“The result is:” +c);
}
catch (ArithmenticException e)
{
//catch divide by zero error
System.out.println(“Division by zero”); }
System.out.println(“After catch statement”);
}
}
Output
Division by zero
After catch statement
As we can see the print statement in the TRY block is not executed. The program control is transferred to catch clause.
· Exception:
o This class specifies the exceptional conditions that your programs should catch. The subclass of this class will contain the custom exception types created by the program.
o Another important subclass of this exception class is RuntimeException. This subclass contains the exceptions that are automatically defined for programs and includes exceptions such as divide by zero and invalid array indexing.
· Error:
o This class defines exceptions that are not expected to be caught under normal circumstances by your program. Java run-time system uses exceptions of the type Error to indicate errors that are related to the run-time environment.
o An example for such an error is, stack overflow.
In Java, exception handling is performed using five keywords, they are: try, catch, throw, throws, and ‘‘Finally’’. The general exception block is as follows:
try{
//block of code to monitor for errors
}
Catch (ExceptionType1 ex1){
//exception handler for ExceptionType1
}
Catch (ExceptionType2 ex2){
//exception handler for ExceptionType2
}
....
Finally {
//block of code to be executed after the try block ends
}
Example:
Exceptions handling using try and catch:
The program statements that you want to monitor for exceptions are specified within the try block. Immediately after a try block, a catch block must be defined. This catch clause specifies the exception type that you want to catch.class HandleException {
public static void main (String args[]) {
int a, b, c;
try {
//monitor a block of code.
a=0;
b=45;
c=b/a;
System.out.println(“The result is:” +c);
}
catch (ArithmenticException e)
{
//catch divide by zero error
System.out.println(“Division by zero”); }
System.out.println(“After catch statement”);
}
}
Output
Division by zero
After catch statement
As we can see the print statement in the TRY block is not executed. The program control is transferred to catch clause.
No comments:
Post a Comment