Today, we were taught about Exception Handling. An exception is a run-time error. Exception Handling is managed by 5 keywords - try, throw, throws, catch and finally. All exception types are subclass of built-in class "Throwable". This concept has 2 advantages:-
*We can fix errors ourselves.
*It prevents programs from automatically terminating.
Example:-
public class ExceptionHandling {
public static void main(String args[])
{
int a,d;
try{
d=11;
a=d/0;
System.out.println("This wont be printed.");
}
catch(ArithmeticException e)
{
System.out.println("Division by Zero");
}
System.out.println("This is an after Catch statement");
}
}
OUTPUT:
Division by Zero
This is an after Catch statement
*We can fix errors ourselves.
*It prevents programs from automatically terminating.
Example:-
public class ExceptionHandling {
public static void main(String args[])
{
int a,d;
try{
d=11;
a=d/0;
System.out.println("This wont be printed.");
}
catch(ArithmeticException e)
{
System.out.println("Division by Zero");
}
System.out.println("This is an after Catch statement");
}
}
OUTPUT:
Division by Zero
This is an after Catch statement