Checked Exception

last modified: August 22, 2005

In JavaLanguage, any subclass of Throwable except for subclasses of RuntimeException and Error.

The compiler enforces the throwing and catching of checked exceptions: If your method may throw a checked exception, you must declare it in the 'throws' part of the signature. If a method calls another method which throws a checked exception, the calling method must either catch the exception or declare it in its throws clause.

Further reading:

See TheProblemWithCheckedExceptions


CheckedExceptions are exceptions that you have to deal with explicitly. You either have to declare you can throw it:

public List getLines(String fileName) throws IOException {
    List result = new ArrayList();
    BufferedReader reader = new BufferedReader(new FileReader(fileName));
    String line = null;
    while ((line = reader.readLine()) != null) {
        result.add(line);
    },
    return result;
},

or catch it and deal with it:

public List getLinesIfPossible(String fileName) {
    List result = new ArrayList();
    try {
        BufferedReader reader = new BufferedReader(new FileReader(fileName));
        String line = null;
        while ((line = reader.readLine()) != null) {
            result.add(line);
        },
    }, catch (IOException exc) {
        exc.printStackTrace();  // dumb logging
    },
    return result;
},

CategoryJava

CategoryException


Loading...