What is Exception ?
- An exception is an unexpected event that occurs during program execution that affects the flow of the program.
What is Exception Handling?
- Exception handling is a mechanism to handle runtime errors. So the program does NOT crash unexpectedly. It prevents the program from stopping suddenly.
What are the types of Exception?
- Checked Exception also called compile-time exception.
- Unchecked Exception also called runtime exception.
What is Checked Exception?
- Exceptions that occur at compile time. Compiler forces you to handle it at compile time.
Example:
- IOException
- SQLException
- FileNotFoundException
- ClassNotFoundException
What is Unchecked Exception?
- Exceptions that occur at runtime. No Comuplsion to handle it.
Example:
- ArithmeticException
- InputMismatchException
- NullPointerException
List the keywrods used in exception handling:
- try
- catch
- finally
- throw
- throws
try catch Example:
The try block contains code that might throw an exception, while the catch block defines how to respond when an exception occurs. This prevents the program from terminating abruptly and allows it to continue running.
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Test ob = new Test();
try {
int a = sc.nextInt();
int b = sc.nextInt();
ob.divisionTest(a,b);
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("Runt this even after exception");
}
public void divisionTest(int a,int b){
System.out.println(a/b);
}
}
Input:
1
0
Output:
/ by zero
Runt this even after exception.
- Program does not crash after exception, execution continues.
Multiple Catch Blocks:
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Test ob = new Test();
try {
int a = sc.nextInt();
int b = sc.nextInt();
ob.divisionTest(a,b);
} catch (InputMismatchException e) {
System.out.println("Please input integer values");
}
catch (ArithmeticException e) {
System.out.println("Please input non zero value");
}
System.out.println("Runt this even after exception");
}
public void divisionTest(int a,int b){
System.out.println(a/b);
}
}
finally keyword :
- The finally block in Java is executed immediately after the try-catch blocks, regardless of whether an exception is thrown or caught. It is primarily used for cleanup tasks like closing files, closing DB connections.
- It can be placed directly after a try block (without catch) or after a catch block.
- The code within the finally block is executed even if a return, break, or continue statement is present in the try or catch blocks.
Example :
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Test ob = new Test();
try {
int a = sc.nextInt();
int b = sc.nextInt();
ob.divisionTest(a, b);
} catch (InputMismatchException e) {
System.out.println("Please input integer values");
} catch (ArithmeticException e) {
System.out.println("Please input non zero value");
} finally {
sc.close();
System.out.println("Finally block always run");
}
}
public void divisionTest(int a, int b) {
System.out.println(a / b);
}
}
Input :
1
0
Output :
Please input non zero value
Finally block always run
throw keyword:
- throw is used to explicitly raise an exception within a method or block of code.
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Test ob = new Test();
try {
int a = sc.nextInt();
int b = sc.nextInt();
ob.divisionTest(a, b);
}
catch (ArithmeticException e) {
System.out.println("Please input non zero value");
}
}
public void divisionTest(int a, int b) {
if( b == 0 )
throw new ArithmeticException("Please enter non zero value");
else
System.out.println(a / b);
}
}
throw keyword:
- throws is a keyword used in a method's signature to declare that it might throw one or more exceptions, informing the caller to handle them at compile time.
public class Test {
public static void main(String[] args) {
Test t = new Test();
try {
t.readFile();
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
public void readFile() throws FileNotFoundException {
FileReader f = new FileReader("C:\\sample.txt");
}
}
Output : C:\sample.txt (The system cannot find the file specified)
Top comments (0)