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

Regarding class files

Class files:- Regarding class file or dot class file also known as byte code of class, in a java programming language we can use any number of classes in our program. After compilation all classes convert into individual   .class   file. Example:- class A { } class B { } class C { } class Student { public static void main(String[]args) { System.out.println("Well-come to my java blog"); } } We can save above program to any   classname.java   or we can use any other   name.java,   there is no compulsion for java file name. But recommended to save by that class name which having main method. I saved the above file to   Student.java   name .   Before compilation my   C:\JAVATECH   folder contain only one java file. Student.java For  compilation. C:\JAVATECH>javac  Student.java After compilation   my   C:\JAVATECH   folder contains five files, on...

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