java eh
java eh
of a program. When an Exception occurs the normal flow of the program is disrupted
and the program/Application terminates abnormally
Error:
------
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError
etc.
2) Unchecked Exception
The classes which inherit RuntimeException are known as unchecked exceptions e.g.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.
Unchecked exceptions are not checked at compile-time, but they are checked at
runtime.
____________________________________________________
try:
The "try" keyword is used to specify a block where we should place exception
code. The try block must be followed by either catch and/or finally.
catch:
The "catch" block is used to handle the exception. It must be preceded by try
block which means we can't use catch block alone. It can be followed by finally
block later.
finally:
The "finally" block is used to execute the important code of the program. It
is executed whether an exception is handled or not.
throw:
The "throw" keyword is used to raise an exception programatically. throw
keyword is mainly used to raise user defined exceptions.
throws:
The "throws" keyword is used to declare exceptions. It doesn't throw an
exception. It specifies that there may occur an exception in the method. It is
always used with method signature.
try-catch:-
-----------
try{
statement(s);
}catch(<ExceptionType> <referencevariable>){
handling_statement(s);
}catch(<ExceptionType> <referencevariable>){
handling_statement(s);
}
-----------------------
-----------------------
__________________________________________________________
try-catch-finally:
------------------
try{
statement(s);
}catch(<ExceptionType> <referenceVariable>){
handling_statement(s);
}catch(<ExceptionType> <referenceVariable>){
handling_statement(s);
}
-----------------------
-----------------------
finally{
statement(s);
}
________________________________________________________________
try-finally:
------------------
try{
statement(s);
}finally{
statement(s);
}
__________________________________________________________________
example:
--------
class Program{
public static void main(String[] args){
int a=10, b=0,c;
c=a/b;
System.out.println("Division = "+c);
}
}
o/p:
---
D:\javaex>java Program
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Program.main(Program.java:4)
_________________________________________________________________
applying try-catch:
------------------
class Program{
public static void main(String[] args){
int a=10, b=0,c;
try{
c=a/b;
}catch(ArithmeticException ex){
System.out.println("Denominator can't be zero!");
c=0;
}
System.out.println("Division = "+c);
}
}
__________________________________________________________________
try with multiple catch clauses:
--------------------------------
class Div{
public static void main(String[] args){
int a, b, c;
try{
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
c=a/b;
System.out.println("Division = "+c);
}catch(ArrayIndexOutOfBoundsException ex){
System.out.println("Usage: java Div num1 num2");
}catch(NumberFormatException ex){
System.out.println("Input Should be Integers");
}catch(ArithmeticException ex){
System.out.println("Denominator can't be zero!");
}
}
}
___________________________________________________________________
Nested / inner try-catch;
------------------------
class Div{
public static void main(String[] args){
int a, b, c;
try{
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
try{
c=a/b;
System.out.println("Division = "+c);
}catch(ArithmeticException ex){
System.out.println("Denominator can't be zero!");
}
}catch(ArrayIndexOutOfBoundsException ex){
System.out.println("Usage: java Div num1 num2");
}catch(NumberFormatException ex){
System.out.println("Input Should be Integers");
}
}
}
___________________________________________________________________
the super "Exception" class:
----------------------------
-> the Exception catch block should be the only catch block or it should be the
last catch block
class Div{
public static void main(String[] args){
int a, b, c;
try{
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
c=a/b;
System.out.println("Division = "+c);
}catch(ArithmeticException ex){
System.out.println("Denominator can't be zero!");
}catch(Exception ex){
System.out.println("Exception Caught: "+ex.getMessage());
}
}
}
___________________________________________________________________