Exception
Handling:-Exception is a class in java which is child class of
Throwable class and Throwable is child class of Object class. Exception is a
kind of error which is occurred by the mistake of programmer or developer and
exception is recoverable. So for the exception developer is responsible. In a
java when the thing is not declare or not exist and we are try to access such
thing than on the particular that place exception object automatically created
and that object is disturb the flow of program and terminate the program
abnormally.
The purpose of the exception handling is that to manage
the flow of the program, program should terminate normally and ignore that part
only where exception occurred. For managing such type of facility we have to use.
try-catch or finally block
Syntax
for
try-catch
try
{
}
catch(Exception e)
{
}
We
can use multiple catch block
try
{
}
catch(Exception1 e)
{
}
catch(Exception2 e)
{
}
catch(Exception3 e)
{
}
But in catch block we have to maintain child to
parent order for exception type means in the above syntax Exception1, Exception2
must be child class and Exception3 must be parent class. We have to write risky
code where chance of exception may occurs inside try block and handling code
inside catch block.
Need
of exception handling:-
Example1:-Test.java
class Test
{
public static void main(String[]args)
{
System.out.println("Hello1");
System.out.println("Hello2");
System.out.println(10/0);
System.out.println("Hello3");
System.out.println("Hello4");
}
}
Output:-
C:\JAVATECH>javac
Test.java
C:\JAVATECH>java Test
Hello1
Hello2
Exception in thread "main"
java.lang.ArithmeticException: / by zero
at
Test.main(Test.java:7)
Example2:-Test.java
class Test
{
public static void main(String[]args)
{
System.out.println("Hello1");
System.out.println("Hello2");
System.out.println(args[0]);
System.out.println("Hello3");
System.out.println("Hello4");
}
}
Output:-
C:\JAVATECH>javac Test.java
C:\JAVATECH>java Test
Hello1
Hello2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at
Test.main(Test.java:7)
Example3:-Test.java
Output:-
C:\JAVATECH>javac Test.java
C:\JAVATECH>java Test
Hello1
Hello2
Exception in thread "main" java.lang.NumberFormatException: For
input string: "I
ndore"
at
java.lang.NumberFormatException.forInputString(Unknown Source)
at
java.lang.Integer.parseInt(Unknown Source)
at
java.lang.Integer.parseInt(Unknown Source)
at
Test.main(Test.java:7)
Example4:-Test.java
class Test
{
public static void main(String[]args)
{
String
s=null;
System.out.println("Hello1");
System.out.println("Hello2");
System.out.println(s.length());
System.out.println("Hello3");
System.out.println("Hello4");
}
}
Output:-
C:\JAVATECH>javac Test.java
C:\JAVATECH>java Test
Hello1
Hello2
Exception in thread "main" java.lang.NullPointerException
at
Test.main(Test.java:8)
Example5:-Test.java
class Test
{
public static void main(String[]args)
{
System.out.println("Hello1");
System.out.println("Hello2");
try
{
System.out.println(10/0);
}
catch(Exception
e)
{
System.out.println(e);
}
System.out.println("Hello3");
System.out.println("Hello4");
}
}
Output:-
C:\JAVATECH>javac Test.java
C:\JAVATECH>java Test
Hello1
Hello2
java.lang.ArithmeticException: / by zero
Hello3
Hello4
Example6:-Test.java
class Test
{
public static void main(String[]args)
{
System.out.println("Hello1");
System.out.println("Hello2");
try
{
System.out.println(10/0);
}
catch(Exception
e)
{
System.out.println(e);
}
System.out.println("Hello3");
System.out.println("Hello4");
}
}
Output:-
C:\JAVATECH>javac
Test.java
C:\JAVATECH>java Test
Hello1
Hello2
java.lang.ArrayIndexOutOfBoundsException: 0
Hello3
Hello4
Example7:-Test.java
class Test
{
public static void main(String[]args)
{
String s=null;
System.out.println("Hello1");
System.out.println("Hello2");
try
{
System.out.println(s.length());
}
catch(Exception
e)
{
System.out.println(e);
}
System.out.println("Hello3");
System.out.println("Hello4");
}
}
Output:-
C:\JAVATECH>javac Test.java
C:\JAVATECH>java Test
Hello1
Hello2
java.lang.NullPointerException
Hello3
Hello4
Example8:-Test.java
class Test
{
public static void main(String[]args)
{
System.out.println("Hello1");
System.out.println("Hello2");
try
{
System.out.println(Integer.parseInt(“indore”));
}
catch(Exception
e)
{
System.out.println(e);
}
System.out.println("Hello3");
System.out.println("Hello4");
}
}
Output:-
C:\JAVATECH>javac Test.java
C:\JAVATECH>java Test
Hello1
Hello2
java.lang.NumberFormatException: For input string:
"indore"
Hello3
Hello4
Example9:-Test.java
class Test
{
public static void main(String[]args)
{
String s=null;
System.out.println("Hello1");
System.out.println("Hello2");
try
{
System.out.println(args[0]);
}
catch(ArithmeticException e)
{
System.out.println(e);
System.out.println("Safely handled by
ArithmeticException class");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
System.out.println("Safely handled by
ArrayIndexOutOfBoundsException class");
}
catch(NullPointerException e)
{
System.out.println(e);
System.out.println("Safely handled by
NullPointerException class");
}
catch(NumberFormatException e)
{
System.out.println(e);
System.out.println("Safely handled by
NumberFormatException class");
}
catch(Exception e)
{
System.out.println(e);
System.out.println("Safely handled by Exception
class");
}
System.out.println("Hello3");
System.out.println("Hello4");
}
}
Output:-
C:\JAVATECH>javac Test.java
C:\JAVATECH>java Test
Hello1
Hello2
java.lang.ArithmeticException: / by zero
Safely handled by ArithmeticException class
Hello3
Hello4
Example10:-Test.java
class Test
{
public static void main(String[]args)
{
String s=null;
System.out.println("Hello1");
System.out.println("Hello2");
try
{
System.out.println(args[0]);
}
catch(ArithmeticException e)
{
System.out.println(e);
System.out.println("Safely handled by
ArithmeticException class");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
System.out.println("Safely handled by
ArrayIndexOutOfBoundsException class");
}
catch(NullPointerException e)
{
System.out.println(e);
System.out.println("Safely handled by
NullPointerException class");
}
catch(NumberFormatException e)
{
System.out.println(e);
System.out.println("Safely handled by
NumberFormatException class");
}
catch(Exception e)
{
System.out.println(e);
System.out.println("Safely handled by Exception
class");
}
System.out.println("Hello3");
System.out.println("Hello4");
}
}
Output:-
C:\JAVATECH>javac Test.java
C:\JAVATECH>java Test
Hello1
Hello2
java.lang.ArrayIndexOutOfBoundsException: 0
Hello3
Hello4
Example11:-Test.java
class Test
{
public static void main(String[]args)
{
String s=null;
System.out.println("Hello1");
System.out.println("Hello2");
try
{
System.out.println(s.length());
}
catch(ArithmeticException e)
{
System.out.println(e);
System.out.println("Safely handled by ArithmeticException
class");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
System.out.println("Safely handled by
ArrayIndexOutOfBoundsException class");
}
catch(NullPointerException e)
{
System.out.println(e);
System.out.println("Safely handled by
NullPointerException class");
}
catch(NumberFormatException e)
{
System.out.println(e);
System.out.println("Safely handled by
NumberFormatException class");
}
catch(Exception e)
{
System.out.println(e);
System.out.println("Safely handled by Exception
class");
}
System.out.println("Hello3");
System.out.println("Hello4");
}
}
Output:-
C:\JAVATECH>javac Test.java
C:\JAVATECH>java Test
Hello1
Hello2
java.lang.NullPointerException
Safely handled by NullPointerException class
Hello3
Hello4
Example12:-Test.java
class Test
{
public static void main(String[]args)
{
String s=null;
System.out.println("Hello1");
System.out.println("Hello2");
try
{
System.out.println(Integer.parseInt("indore"));
}
catch(ArithmeticException e)
{
System.out.println(e);
System.out.println("Safely handled by
ArithmeticException class");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
System.out.println("Safely handled by
ArrayIndexOutOfBoundsException class");
}
catch(NullPointerException e)
{
System.out.println(e);
System.out.println("Safely handled by
NullPointerException class");
}
catch(NumberFormatException e)
{
System.out.println(e);
System.out.println("Safely handled by
NumberFormatException class");
}
catch(Exception e)
{
System.out.println(e);
System.out.println("Safely handled by Exception
class");
}
System.out.println("Hello3");
System.out.println("Hello4");
}
}
Output:-
C:\JAVATECH>javac Test.java
C:\JAVATECH>java Test
Hello1
Hello2
java.lang.NumberFormatException: For input string:
"indore"
Safely handled by NumberFormatException class
Hello3
Hello4
Example13:-
class Test
{
public static void main(String[]args)
{
String s=null;
try
{
System.out.println(10/0);
}
catch(Exception e)
{
System.out.println(e);
try
{
System.out.println(args[0]);
}
catch(Exception e1)
{
System.out.println(e1);
try
{
System.out.println(s.length());
}
catch(Exception e2)
{
System.out.println(e2);
try
{
System.out.println(Integer.parseInt("indore"));
}
catch(Exception e3)
{
System.out.println(e3);
}
}
}
}
}
}
Output:-
C:\JAVATECH>javac Test.java
C:\JAVATECH>java Test
java.lang.ArithmeticException: / by zero
java.lang.ArrayIndexOutOfBoundsException: 0
java.lang.NullPointerException
java.lang.NumberFormatException: For input string:
"indore"
Comments
Post a Comment