Exception Handling in Java
Exception Handling in Java
1) Checked Exception
Keyword Description
try The "try" keyword is used to specify a block where we should place an exception code. .
finally The "finally" block is used to execute the necessary code of the program.
Throw Exception:
In Java, exceptions allows us to write good quality codes where
the errors are checked at the compile time instead of runtime
and we can create custom exceptions making the code recovery
and debugging easier.
throw IOException :
Bulit in Exceptions:
There are given some scenarios where unchecked exceptions
may occur. They are as follows:
1) ArithmeticException:
1. int a=50/0;//ArithmeticException
2) NullPointerException:
1. String s=null;
2. System.out.println(s.length());//NullPointerException
3) NumberFormatException:
1. String s="abc";
2. int i=Integer.parseInt(s);//NumberFormatException
4) ArrayIndexOutOfBoundsException:
Example 2:
TestCustomException:
1. try{
2. //code that may throw an exception
3. }
4. catch(Exception_class_Name ref)
5. {
6. }
1. try{
2. //code that may throw an exception
3. }
4. finally{
5. }
Output:
Exception in thread "main"
java.lang.ArithmeticException: / by zero
Example 2
Output:
java.lang.ArithmeticException: / by zero
rest of the code
Example 3
Output:
java.lang.ArithmeticException: / by zero
if an exception occurs in the try block, the rest of the block code
will not execute.
Example 4
Output:
java.lang.ArithmeticException: / by zero
rest of the code
Example 5
Output:
Can't divided by zero
Throw Keyword:
Output:
.
1. import java.io.*;
2.
3. public class TestThrow2 {
4.
5. //function to check if person is eligible to vote or not
6. public static void method() throws FileNotFoundException {
7.
8. FileReader file = new FileReader("C:\\Users\\Anurati\\Desk
top\\abc.txt");
9. BufferedReader fileInput = new BufferedReader(file);
10.
11.
12. throw new FileNotFoundException();
13.
14. }
15. //main method
16. public static void main(String args[]){
17. try
18. {
19. method();
20. }
21. catch (FileNotFoundException e)
22. {
23. e.printStackTrace();
24. }
25. System.out.println("rest of the code...");
26. }
27. }
Output:
Output:
Parameters:
Byte Streams:
Java byte streams are used to perform input and output of 8-bit
bytes. Though there are many classes related to byte streams
but the most frequently used classes
are, FileInputStream and FileOutputStream.
Example
import java.io.*;
public class CopyFile {
try {
in = new FileInputStream("input.txt");
out = new FileOutputStream("output.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
}
finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
Character Streams:
Java Byte streams are used to perform input and output of 8-bit
bytes, whereas Java Character streams are used to perform
input and output for 16-bit Unicode.
Example
import java.io.*;
public class CopyFile {
try {
in = new FileReader("input.txt");
out = new FileWriter("output.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
}finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}