Exception handling in java is a way to catch error or exception
Exception is an event that can occur during the execution of the program.
We use try catch and final to catch error.
When an error occur in a method it creates an object of the error and send it to the runtime system it is called throwing an exception.
There are two kind of error that can be caught by catch but runtime exception can not be caught by catch.
The type which can be caught are
1.Checked Exception
2.Error
import java.io.*;
import java.util.List;
import java.util.ArrayList;
public class ListOfNumbers {
private List<Integer> list;
private static final int SIZE = 10;
public ListOfNumbers () {
list = new ArrayList<Integer>(SIZE);
for (int i = 0; i < SIZE; i++) {
list.add(new Integer(i));
}
}
public void writeList() {
// The FileWriter constructor throws IOException, which must be caught.
PrintWriter out = new PrintWriter(new FileWriter("OutFile.txt"));
for (int i = 0; i < SIZE; i++) {
// The get(int) method throws IndexOutOfBoundsException, which must be caught.
out.println("Value at: " + i + " = " + list.get(i));
}
out.close();
}
}
0 Comment(s)