You are on page 1of 41

Exception Handling

What is Exception?
Exception Handling
An exception can occur for following
reasons.

•User error

•Programmer error

•Physical error
1 import java.util.*;
2 public class MyClass
3 {
4 public static void main(String args[])
5 {
6 int x=10;
7 int y=0;
8 int z=x/y;
9 System.out.println(z);
10 }
11 }
12
13
14
15
Output
Exception in thread "main" java.lang.ArithmeticException: / by zero at
MyClass.main(MyClass.java:6)
1 import java.io.*;
2 public class Main {
3 public static void main(String[] args){
4 System.out.println("First line");
5 System.out.println("Second line");
6 int[] myIntArray = new int[]{1, 2, 3};
7 print(myIntArray);
8 System.out.println("Third line");
9 }
10 public static void print(int[] arr) {
11 System.out.println(arr[3]);
12 System.out.println("Fourth element successfully displayed!");
13 }
14 }
15
Output
First line
Second line
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Main.print(Main.java:11)
at Main.main(Main.java:7)
Hierarchy of Java Exception classes
Exception IOException

SQLException

Throwable ClassNot
FoundException

RuntimeException

Error
Types of Exception
•Checked Exception
•Unchecked Exception
1 //Example for Checked exception
2 import java.io.FileInputStream;
3 public class Main {
4 public static void main(String[] args) {
5 FileInputStream fis = null;
6 fis = new FileInputStream(“D:/myfile.txt");
7 int k;
8 while(( k = fis.read() ) != -1)
9 {
10 System.out.print((char)k);
11 }
12 fis.close();
13 }
14 }
15
Output
Exception in thread "main" java.lang.Error: Unresolved compilation
problems:
Unhandled exception type FileNotFoundException
Unhandled exception type IOException
Unhandled exception type IOException
1 //Example for Unchecked exception
2 import java.io.*;
3 public class Main {
4 public static void main(String[] args){
5 int[] arr = new int[]{7, 11, 30, 63};
6 System.out.println(arr[5]);
7 }
8 }
9
10
11
12
13
14
15
Output
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Main.main(Main.java:5)
Exception Handling
Exception Handling?

It is the process of responding to the


occurrence during computation of
exceptions.
Exceptional Handling Keywords
•try
•catch
•finally
•throw
•throws
1 import java.util.*; 1 import java.util.*;
2 public class MyClass 2 public class MyClass
3 { 3 {
4 public static void main(String 4 public static void main(String
5 args[]) 5 args[])
6 { 6 {
7 int x=10; 7 int x=10;
8 int y=0; 8 int y=0;
9 int z=x/y; 9 try
10 System.out.println(z); 10 {
11 } 11 int z=x/y;
12 } 12 }
13 13 catch(Exception e)
14 14 {
15 15 System.out.print(e);
16 16 }
17 17 }
18 18 }
19 19
20 20
21 21
22 22
1 import java.util.*; comment/pseudo code/output
2 public class MyClass Output:
3 { java.lang.ArithmeticException: /
4 public static void main(String by zero
5 args[])
6 {
7 int x=10;
8 int y=0; Syntax:
9 try try
10 { {
11 int z=x/y; // Protected code
12 } }
13 catch(Exception e) catch (ExceptionName e1)
14 { {
15 System.out.print(e); // Catch block
16 } }
17 }
18 }
19
20
21
22
1 import java.io.*;
2 public class Main {
3 public static void main(String[] args){
4 System.out.println("First line");
5 System.out.println("Second line");
6 try{
7 int[] myIntArray = new int[]{1, 2, 3};
8 print(myIntArray);
9 }
10 catch (ArrayIndexOutOfBoundsException e){
11 System.out.println("The array doesn't have fourth element!");
12 }
13 System.out.println("Third line");
14 }
15 public static void print(int[] arr) {
16 System.out.println(arr[3]);
17 }
18 }
19
20
21
22
OUTPUT

First line
Second line
The array doesn't have fourth element!
Third line
Internal Working of try-catch Block
System.out.print(arr[3]) Exception object
An object of
exception class is
thrown
Is
Handled?

no yes
JVM
• Prints out exception rest of code is
Description executed
• Prints stack trace
• Terminates the
program
1 //Predict the Output
2 import java.util.*;
3 public class MyClass
4 {
5 public static void main(String args[])
6 {
7 MyClass ob = new MyClass();
8 try
9 {
10 ob.meth1();
11 }
12 catch(ArithmeticException e)
13 {
14 System.out.println("ArithmaticException thrown by meth1()
15 method is caught in main() method");
16 }
17 }
18
19
20
21
22
1 public void meth1()
2 {
3 try
4 {
5 System.out.println(100/0);
6 }
7 catch(NullPointerException nullExp)
8 {
9 System.out.println("We have an Exception - "+nullExp);
10 }
11 System.out.println("Outside try-catch block");
12 }
13 }
14
15
16
17
18
19
20
21
22
1 import java.util.*;
2 public class MyClass {
3 public static void main(String[] args) {
4 try{
5 int arr[]=new int[5];
6 arr[7]=100/0;
7 }
8 catch(ArithmeticException e)
9 {
10 System.out.println("Arithmetic Exception occurs");
11 }
12 catch(ArrayIndexOutOfBoundsException e)
13 {
14 System.out.println("ArrayIndexOutOfBounds Exception occurs");
15 }
16 catch(Exception e)
17 {
18 System.out.println("Parent Exception occurs");
19 }
20 System.out.println("rest of the code");
21 }
22 }
1 //Predict the Output
2 import java.util.*;
3 public class MyClass
4 {
5 public static void main(String[] args)
6 {
7 String[] s = {"Hello", "423", null, "Hi"};
8 for (int i = 0; i < 5; i++)
9 {
10 try
11 {
12 int a = s[i].length() + Integer.parseInt(s[i]);
13 }
14 catch(NumberFormatException ex)
15 {
16 System.out.println("NumberFormatException");
17 }
18
19
20
21
22
1 catch (ArrayIndexOutOfBoundsException ex)
2 {
3 System.out.println("ArrayIndexOutOfBoundsException");
4 }
5 catch (NullPointerException ex)
6 {
7 System.out.println("NullPointerException");
8 }
9 System.out.println("After catch, this statement will be
10 executed");
11 }
12 }
13 }
14
15
16
17
18
19
20
21
22
1 // Pipe(|) operator
2 import java.util.*;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 String[] s = {"abc", "123", null, "xyz"};
8 for (int i = 0; i < 5; i++)
9 {
10 try
11 {
12 int a = s[i].length() + Integer.parseInt(s[i]);
13 }
14 catch(NumberFormatException | NullPointerException |
15 ArrayIndexOutOfBoundsException ex)
16 {
17 System.out.println("Handles above mentioned three
18 Exception");
19 }
20 }
21 }
22 }
1 //Predict the output
2 import java.util.*;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 try
8 {
9 int i = Integer.parseInt("Thor");
10 }
11 catch(Exception ex)
12 {
13 System.out.println("This block handles all exception types");
14 }
15 catch(NumberFormatException ex)
16 {
17 System.out.print("This block handles NumberFormatException");
18 }
19 }
20 }
21
22
1 //Predict the OUtput
2 import java.util.*;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 String[] str = {null, "Marvel"};
8 for (int i = 0; i < str.length; i++)
9 {
10 try
11 {
12 int a = str[i].length();
13 try
14 {
15 a = Integer.parseInt(str[i]);
16 }
17 catch (NumberFormatException ex)
18 {
19 System.out.println("NumberFormatException");
20 }
21 }
22
1 catch(NullPointerException ex)
2 {
3 System.out.println("NullPointerException");
4 }
5 }
6 }
7 }
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1 //Syntax for finally block Description
2
3 try { It contains all the crucial
4 //Statements that may cause an statements that must be executed
5 exception whether exception occurs or not.
6 }
7 catch {
8 //Handling exception
9 }
10 finally {
11 //Statements to be executed
12 }
13
14
15
16
17
18
19
20
21
22
1 public class MyClass{
2 public static void main(String args[]){
3 try{
4 int data = 30/3;
5 System.out.println(data);
6 }
7 catch(NullPointerException e)
8 {
9 System.out.println(e);
10 }
11 finally
12 {
13 System.out.println("finally block is always executed");
14 }
15 }
16 }
17
18
19
20
21
22
1 public class MyClass
2 {
3 public static void main(String args[]) {
4 try{
5 int num=121/0;
6 System.out.println(num);
7 }
8 catch(ArithmeticException e){
9 System.out.println("Number should not be divided by zero");
10 }
11 finally{
12 System.out.println("This is finally block");
13 }
14 System.out.println("Out of try-catch-finally");
15 }
16 }
17
18
19
20
21
22
1 public class MyClass
2 {
3 public static void main(String args[])
4 {
5 System.out.println(MyClass.myMethod());
6 }
7 public static int myMethod()
8 {
9 try
10 {
11 return 0;
12 }
13 finally
14 {
15 System.out.println("This is Finally block");
16 System.out.println("Finally block ran even after return
17 statement");
18 }
19 }
20 }
21
22
1 public class MyClass
2 {
3 public static void main(String args[])
4 {
5 System.out.println(MyClass.myMethod());
6 }
7 public static int myMethod()
8 {
9 try
10 {
11 return 0;
12 }
13 finally
14 {
15 System.out.println("This is Finally block");
16 System.out.println("Finally block ran even after return
17 statement");
18 }
19 }
20 }
21
22
Cases when the finally block doesn’t execute

• The death of a Thread.


• Using of the System. exit() method.
• Due to an exception arising in the finally block.
1 public class MyClass{
2 public static void main(String args[])
3 {
4 System.out.println(MyClass.myMethod());
5 }
6 public static int myMethod(){
7 try {
8 int x = 63;
9 int y = 9;
10 int z=x/y;
11 System.out.println("Inside try block");
12 System.exit(0);
13 }
14 catch (Exception exp){
15 System.out.println(exp);
16 }
17 finally{
18 System.out.println("Java finally block");
19 return 0;
20 }
21 }
22 }
1 public class MyClass{
2 public static void main(String args[])
3 {
4 System.out.println(MyClass.myMethod());
5 }
6 public static int myMethod(){
7 try {
8 int x = 63;
9 int y = 0;
10 int z=x/y;
11 System.out.println("Inside try block");
12 System.exit(0);
13 }
14 catch (Exception exp){
15 System.out.println(exp);
16 }
17 finally{
18 System.out.println("Java finally block");
19 return 0;
20 }
21 }
22 }
1 //Syntax for throw block Description
2
3 The Java throw keyword is used
4 throw new exception_class("error message"); to explicitly throw an
5 exception.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1 public class MyClass
2 {
3 public static void validate(int age)
4 {
5 if(age<21 || age>27)
6 throw new ArithmeticException("not eligible");
7 else
8 System.out.println("Eligible to attend Military Selection ");
9 }
10 public static void main(String args[])
11 {
12 validate(30);
13 System.out.println("rest of the code...");
14 }
15 }
16
17
18
19
20
21
22
OUTPUT

Exception in thread "main" java.lang.ArithmeticException: not eligible


at MyClass.validate(MyClass.java:6)
at MyClass.main(MyClass.java:12)
1 public class MyClass
2 {
3 public static void validate(int age)
4 {
5 if(age<21 || age>27)
6 throw new ArithmeticException("not eligible");
7 else
8 System.out.println("Eligible to attend Military Selection ");
9 }
10 public static void main(String args[])
11 {
12 try
13 {
14 validate(30);
15 }
16 catch(ArithmeticException e)
17 {
18 System.out.println(e);
19 }
20 System.out.println("rest of the code...");
21 }
22 }
OUTPUT

java.lang.ArithmeticException: not eligible


rest of the code...
1 public class MyClass
2 {
3 public static void validate(int age)
4 {
5 if(age<21 || age>27)
6 throw new ArithmeticException("not eligible");
7 else
8 System.out.println("Eligible to attend Military Selection ");
9 }
10 public static void main(String args[])
11 {
12 try
13 {
14 validate(21);
15 }
16 catch(ArithmeticException e)
17 {
18 System.out.println(e);
19 }
20 System.out.println("rest of the code...");
21 }
22 }
OUTPUT

Eligible to attend Military Selection


rest of the code...
THANK YOU

You might also like