Exception Handling
Exception Handling
(vthnhan@vnu.edu.vn)
Syntax errors
Subjectively
violation of Java’s grammatical rules wrong
Java code won’t even compile
Runtime errors
which occurs during the execution of a program (at run time) that
disrupts the normal flow of the program’s instructions
NullPointerException is an
example of such an
exception
StackOverflowError is an
example of such an error
The exception object contains name & description of the exception and the
current state of the program where exception has occurred
Creating the Exception Object and handling it to the run-time system is called
throwing an Exception
There might be a list of the methods that had been called to get to the
method where exception was occurred
The run-time system starts searching from the method in which exception
occurred, proceeds through Call Stack in the reverse order in which
methods were called
i.e., the type of the exception object thrown matches the type of the exception
object it can handle
then run-time system hand over the Exception Object to default exception
handler, which is part of run-time system
Name of exception
java.lang.NullPointerException
at ThrowsExecp.main(ThrowsExecp.java:6)
Description
Your code can catch and handle this exception using catch block
Any code that must be executed after a try block completes is put
in a finally block
& provide separate exception handler within a catch block for each exception
ExceptionType must be the name of the class that inherits from Throwable
Often used to put important codes like closing the file or closing the connection
ArithmeticException ArrayIndexOutofBoundException
ClassNotFoundException FileNotFoundException
IOExeption
NoSuchFieldException NoSuchMethodException
Example 1
class StringIndexOutofBound_Ex{
public static void main(String args[]){
try{
String s= “no fun with debugging”; //length=21
char c= s.charAt(21); //accessing 22th element
System.out.println(c);
}
catch(StringIndexOutOfBound e){
System.out.println(e.get);
}
}
}
Output: String index out of range: 21
Example 2
class NumberFormatException_Ex{
public static void main(String[] args){
try{
int num= Integer.parseInt(“abc”);
System.out.println(num);
}
catch(NumberFormatException e){
System.out.println(e.getMessage());
}
}
}
Output: Number format exception
Runtime error
import java.io.*;
class Main{
public static void main(String[] args) {
FileReader file = new FileReader(“C:\\test.txt”);//FileNotFoundException …
BufferReader fileInput = new BufferReader(file);
for(int i=0;i<2;i++) System.out.println(fileInput.readLine()); //IOException…
fileInput.close(); //IOException must be caught, declared or thrown
}
}
24/10/2023 Exception handling Page 29
Checked…
Need to either specify list of exceptions or use catch-throw block
Choose the former and throw the list from the method using throws
Since FileNotFoundException is a subclass of IOException, so we just need to
specify IOException in the throws list and make the program compiler-error-free
import java.io.*;
class Main{
public static void main(String[] args) throws IOException{
FileReader file = new FileReader(“C:\\test.txt”);
BufferReader fileInput = new BufferReader(file);
for(int i=0;i<2;i++) System.out.println(fileInput.readLine());
fileInput.close();
}
}
class Main{
public static void main(String[] args){
int x=10, y=0;
int z = x/y;
}
}