package randomtest;
public class UncheckedTester {
/**
* @param args
*/
public static void main(
String[] args) {
// TODO Auto-generated method stub
int dontDivideMeByZero = 5;
int result = dontDivideMeByZero/0;
}
}
run the above code in a file called UncheckedTester in an appropriate directory etc.... and you will see that it compiles, but throws an exception at runtime (hence runtime exception)
run the below code and you will find that it wont compile because there is an IOException that MAY be thrown, this is checked and an error is reported before it can be compiled, if you remove the '//removed' tags you will find out that it compiles and runs and probably works without exception.
package randomtest;
import java.io.File;
import java.io.FileWriter;
public class CheckedTester {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("WrittenFile.txt");
//removedtry {
FileWriter fileWriter = new FileWriter(file);
fileWriter.write("This is what I am talkin'bout");
removed//} catch (java.io.IOException ioe) {
removed//System.out.println("Exception caught"+ioe);
removed//}
}
}
to explain further this was extracted from here
http://elvis.rowan.edu/~hartley/Courses/OOPDA/Handouts/bj12.html There are two kinds of exception objects: checked and unchecked. Unchecked exceptions are subclasses of the RuntimeException class (either built in to
Java or defined by the user). Checked exceptions are subclasses of the Exception class (that are not subclasses of RuntimeException). RuntimeException is a direct subclass of Exception.
Exceptions built in to Java for null pointers, index values out of bounds, and class cast errors at runtime are of the unchecked kind. Exceptions dealing with file IO (and many others) are of the checked kind. If you define your own exception classes, the guidelines are as follows.
Checked exceptions (extends Exception): for circumstances beyond the program's control, like file does not exist, disk full, etc. These are unpredictable and could occur at any time. The program should continue and try to work around the problem, such as telling the user the file does not exist and prompting for a file name again.
Unchecked exceptions (extends RuntimeException): for circumstances the program could prevent or avoid with more or better coding, like null pointer, index out of bounds, illegal class cast. These should not occur and should be fixed if they do. The program should not continue
further reading here:
http://java.sun.com/docs/books/jls/second_edition/html/exceptions.doc.html