Why does my try / catch only accept "Exception e" (Generic)?

12

I want to specify the exceptions, not this generalized way, however, item.write only accepts Exception e

public File saveFile(FileItem item, File dirFile, String filename) {
        dirFile.mkdirs();
        File file = FileUtils.getFile(dirFile, filename);
        try {
            item.write(file);
            return file;
        } catch (Exception e) {
            throw new InternalServerErrorException(e);
        }
    }
    
asked by anonymous 18.03.2015 / 19:00

2 answers

7

Actually your try-catch does accept other, more specific, types of exceptions, but you can not escape having to treat the more generic Exception or having to add it in the method declaration.

This occurs for these two reasons:

1) Java has something called checked exceptions

The idea of a checked exception is to warn the consumer of a method by signing the method, what exceptions that code can throw, so the consumer explicitly needs to decide what to do with those exceptions.

The consumer options for a method that declares checked exceptions are:

a) Capture the exception and do something with it:

public void facaAlgo() {
    try {
        MetodoQueLancaCertaCheckedException();
    } catch (CertaCheckedException e) {
        // ...faz algo útil com a exceção
    }
}

b) Do nothing with the exception and let it propogate. In this case, the consumer must itself declare that same exception in its own signature as a checked exception:

public void facaAlgo() throws CertaCheckedException {
    MetodoQueLancaCertaCheckedException();
}

Now the consumer of knife method will also have to either catch the exception or redeclare it in its own signature.

2) The FileItem class developer has been relaxed

This is the second reason you are required to either capture the generic exception Exception or redeclare it in your method signature.

This class belongs to the apache-commons library. Who made this class, or precisely this method, decided not to have to decide which exceptions to declare and declared the most generic. All other methods in the class throw more specific exceptions.

How you can handle a more specific exception

So you've noticed that this method can trigger more specific exceptions, such as IO , and want to give a different treatment to this exception. Yes, you can!

Although you can not escape having to do something with Exception , you can do what you need with the most specific exception. If you do not want to declare checked exceptions for the consumer of your code, do so:

public File saveFile(FileItem item, File dirFile, String filename) {
    dirFile.mkdirs();
    File file = null;
    try {
        item.write(file);
        return file;
    } catch (IOException e) {
        // ... tratamento específico para estes tipos de exceção
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

Or, if you wish or do not mind having Exception as a checked exception method, do so:

public File saveFile(FileItem item, File dirFile, String filename) throws Exception {
    dirFile.mkdirs();
    File file = null;
    try {
        item.write(file);
        return file;
    } catch (IOException e) {
        // ... tratamento específico para estes tipos de exceção
    }
}

Conclusion

Your try-catch accepts other types than Exception but due to a (probably bad) decision of the FileItem.write method in>, you'll need to treat Exception too and treat the most specific exception you want.

    
18.03.2015 / 21:20
7

According to javadoc of the FileItem class , this is the expected behavior, because it only sends a Exception when an error occurs, without a more specific type of object.

In short, you can treat Exception by throwing a different type, but it will still be a generic error posting.

    
18.03.2015 / 19:07