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

inheritance

INHERITANCE:- Inheritance is the process of code re-usability, using this property software or applications can develop in less time or can develop rapidly. Inheritance increases the readability of code and decrease redundancy or repetition of code or reduces size of code. Use of previously developed code in future any number of times is called inheritance. Through inheritance variables and methods of class can inherited. Types of Inheritance:- 1)         Single or Single level inheritance 2)         Multilevel inheritance 3)         Hierarchical inheritance Java does not support to multiple inheritance. Single or single level inheritance:- In a single inheritance a class is inherited by a single class. In a single or single level inheritance a sub class having only one super class directly or indirectly. Syntax: -        class Subclass ex...

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...

Abstraction,Encapsulation And Inheritance

Abstraction  is a one of the great feature of Oops,in this feature hide entire details of product, show only requirement specifications(what product will do). The main purpose behind abstraction is to increase productivity or use of product without knowledge of product implementation. in this case the person don't know how product  was implemented but know how product works or action of product. In java abstraction is achieved by abstract class and interface,here interface provide 100% abstraction and abstract class provide partial abstraction. For example: - ATM machine is example of abstraction.  every technical and non-technical person use facility of ATM without knowing the entire process. they know if they click on withdraw button then they can withdraw money from ATM machine but they don't know when they click on withdraw button the request is send to the server and after go into database and so on. Here ATM machine show only requirement specificat...