Skip to main content

Exception Handling

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

Popular posts from this blog

Inner classes

Inner Classes:- class inside class or interface called inner class or nested class. Advantage of inner classes is grouping multiple classes and interfaces into a single class. Another big advantage of inner class is that inner class can happily access all the data or variables of outer class directly. Types of Nested classes:- There are two types of nested classes static and non-static nested classes. (A)Non –static inner classes:- 1)     Member inner class 2)     Anonymous inner class 3)     Local inner class (B)Static nested class Example1:-Outer.java class Outer { class Inner { void disp() { System.out.println("Hello This is Inner class Method"); } } public static void main(String[]args) { Inner in=new Inner(); in.disp(); } } Output:- C:\JAVATECH>javac Outer.java Outer.java:12: error: non-static variable this cannot be referenced from a static context Inner in=new Inner(); ...

Inheritance-4

Example10:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(null); System.out.println("This is sub class Zero argument constructor"); } public static void main(String[]args) { Test t=new Test(); } } Output:- C:\JAVATECH>javac Test.java C:\JAVATECH>java Test This is super class One argument constructor This is sub class Zero argument constructor Example11:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(); super(null); System.out.println("This is su...

Small concept about OOPs

Some small concept about OOPs:- In many books and website write about OOPs concepts as hundred percent object oriented, pure object oriented and partial object oriented language. Hundred percent object oriented:- In the hundred percent object oriented movie hero is object, without object program is incomplete that means can not execute program without object. Without class and object programming or application development is not possible. Example of hundred percent object oriented language is  Smalltalk . Pure object oriented:- In the object oriented cinema obviously object is the hero. In pure object oriented language object having same importance but in this having some relax that is without creating object we can successfully execute our application. So in the object oriented programming without class we cannot create or run our application, class is must. Example of pure object oriented language is  Java . Partial object oriented:- In this type of la...