Exception Handling
What is Exception?
Exception Handling
An exception can occur for following
reasons.
•User error
•Programmer error
•Physical error
1 import [Link].*;
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 [Link](z);
10 }
11 }
12
13
14
15
Output
Exception in thread "main" [Link]: / by zero at
[Link]([Link])
1 import [Link].*;
2 public class Main {
3 public static void main(String[] args){
4 [Link]("First line");
5 [Link]("Second line");
6 int[] myIntArray = new int[]{1, 2, 3};
7 print(myIntArray);
8 [Link]("Third line");
9 }
10 public static void print(int[] arr) {
11 [Link](arr[3]);
12 [Link]("Fourth element successfully displayed!");
13 }
14 }
15
Output
First line
Second line
Exception in thread "main" [Link]: 3
at [Link]([Link])
at [Link]([Link])
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 [Link];
3 public class Main {
4 public static void main(String[] args) {
5 FileInputStream fis = null;
6 fis = new FileInputStream(“D:/[Link]");
7 int k;
8 while(( k = [Link]() ) != -1)
9 {
10 [Link]((char)k);
11 }
12 [Link]();
13 }
14 }
15
Output
Exception in thread "main" [Link]: Unresolved compilation
problems:
Unhandled exception type FileNotFoundException
Unhandled exception type IOException
Unhandled exception type IOException
1 //Example for Unchecked exception
2 import [Link].*;
3 public class Main {
4 public static void main(String[] args){
5 int[] arr = new int[]{7, 11, 30, 63};
6 [Link](arr[5]);
7 }
8 }
9
10
11
12
13
14
15
Output
Exception in thread "main" [Link]: 5
at [Link]([Link])
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 [Link].*; 1 import [Link].*;
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 [Link](z); 10 {
11 } 11 int z=x/y;
12 } 12 }
13 13 catch(Exception e)
14 14 {
15 15 [Link](e);
16 16 }
17 17 }
18 18 }
19 19
20 20
21 21
22 22
1 import [Link].*; comment/pseudo code/output
2 public class MyClass Output:
3 { [Link]: /
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 [Link](e); // Catch block
16 } }
17 }
18 }
19
20
21
22
1 import [Link].*;
2 public class Main {
3 public static void main(String[] args){
4 [Link]("First line");
5 [Link]("Second line");
6 try{
7 int[] myIntArray = new int[]{1, 2, 3};
8 print(myIntArray);
9 }
10 catch (ArrayIndexOutOfBoundsException e){
11 [Link]("The array doesn't have fourth element!");
12 }
13 [Link]("Third line");
14 }
15 public static void print(int[] arr) {
16 [Link](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
[Link](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 [Link].*;
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 [Link]("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 [Link](100/0);
6 }
7 catch(NullPointerException nullExp)
8 {
9 [Link]("We have an Exception - "+nullExp);
10 }
11 [Link]("Outside try-catch block");
12 }
13 }
14
15
16
17
18
19
20
21
22
1 import [Link].*;
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 [Link]("Arithmetic Exception occurs");
11 }
12 catch(ArrayIndexOutOfBoundsException e)
13 {
14 [Link]("ArrayIndexOutOfBounds Exception occurs");
15 }
16 catch(Exception e)
17 {
18 [Link]("Parent Exception occurs");
19 }
20 [Link]("rest of the code");
21 }
22 }
1 //Predict the Output
2 import [Link].*;
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() + [Link](s[i]);
13 }
14 catch(NumberFormatException ex)
15 {
16 [Link]("NumberFormatException");
17 }
18
19
20
21
22
1 catch (ArrayIndexOutOfBoundsException ex)
2 {
3 [Link]("ArrayIndexOutOfBoundsException");
4 }
5 catch (NullPointerException ex)
6 {
7 [Link]("NullPointerException");
8 }
9 [Link]("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 [Link].*;
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() + [Link](s[i]);
13 }
14 catch(NumberFormatException | NullPointerException |
15 ArrayIndexOutOfBoundsException ex)
16 {
17 [Link]("Handles above mentioned three
18 Exception");
19 }
20 }
21 }
22 }
1 //Predict the output
2 import [Link].*;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 try
8 {
9 int i = [Link]("Thor");
10 }
11 catch(Exception ex)
12 {
13 [Link]("This block handles all exception types");
14 }
15 catch(NumberFormatException ex)
16 {
17 [Link]("This block handles NumberFormatException");
18 }
19 }
20 }
21
22
1 //Predict the OUtput
2 import [Link].*;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 String[] str = {null, "Marvel"};
8 for (int i = 0; i < [Link]; i++)
9 {
10 try
11 {
12 int a = str[i].length();
13 try
14 {
15 a = [Link](str[i]);
16 }
17 catch (NumberFormatException ex)
18 {
19 [Link]("NumberFormatException");
20 }
21 }
22
1 catch(NullPointerException ex)
2 {
3 [Link]("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 [Link](data);
6 }
7 catch(NullPointerException e)
8 {
9 [Link](e);
10 }
11 finally
12 {
13 [Link]("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 [Link](num);
7 }
8 catch(ArithmeticException e){
9 [Link]("Number should not be divided by zero");
10 }
11 finally{
12 [Link]("This is finally block");
13 }
14 [Link]("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 [Link]([Link]());
6 }
7 public static int myMethod()
8 {
9 try
10 {
11 return 0;
12 }
13 finally
14 {
15 [Link]("This is Finally block");
16 [Link]("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 [Link]([Link]());
6 }
7 public static int myMethod()
8 {
9 try
10 {
11 return 0;
12 }
13 finally
14 {
15 [Link]("This is Finally block");
16 [Link]("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 [Link]([Link]());
5 }
6 public static int myMethod(){
7 try {
8 int x = 63;
9 int y = 9;
10 int z=x/y;
11 [Link]("Inside try block");
12 [Link](0);
13 }
14 catch (Exception exp){
15 [Link](exp);
16 }
17 finally{
18 [Link]("Java finally block");
19 return 0;
20 }
21 }
22 }
1 public class MyClass{
2 public static void main(String args[])
3 {
4 [Link]([Link]());
5 }
6 public static int myMethod(){
7 try {
8 int x = 63;
9 int y = 0;
10 int z=x/y;
11 [Link]("Inside try block");
12 [Link](0);
13 }
14 catch (Exception exp){
15 [Link](exp);
16 }
17 finally{
18 [Link]("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 [Link]("Eligible to attend Military Selection ");
9 }
10 public static void main(String args[])
11 {
12 validate(30);
13 [Link]("rest of the code...");
14 }
15 }
16
17
18
19
20
21
22
OUTPUT
Exception in thread "main" [Link]: not eligible
at [Link]([Link])
at [Link]([Link])
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 [Link]("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 [Link](e);
19 }
20 [Link]("rest of the code...");
21 }
22 }
OUTPUT
[Link]: 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 [Link]("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 [Link](e);
19 }
20 [Link]("rest of the code...");
21 }
22 }
OUTPUT
Eligible to attend Military Selection
rest of the code...
THANK YOU