简体   繁体   中英

Changing the type of Exception a method throws in Java

What I am trying to do feels counter intuitive, but Java keeps surprising me all the time, so I give it a shot.

I am implementing an application with the help of the ESAPI library. This library provides its own exceptions. In addition to the well known exception behavior those exceptions do things like automatic logging with log4j and providing information to an intrusion detection module.

Now I want to use these features in all parts of my application. Anytime a method of mine throws an exception, I don't throw a normal exception but a selfmade exception that extends from the new EnterpriseSecurityException. Works like a charm.

But what can I do if I use a method, that throws a normal exception? Let's say I want to read a file using an java.io.FileInputStream? It cannot find the file and throws a FileNotFoundException. Unfortunately the FileNotFoundException does not provide the features of my own Exceptions, because it does not extend the EnterpriseSecurityException.

Is there any trick in Java to change the exceptions a method throws? It sounds strange to me as I write this, but maybe someone can come up with a solution. The only idea I had so far is catching the normal exception, extract the relevant information, and build a new exception with it. But this seems pretty crude...

Catch the exception and throw a new one that extends from EnterpriseSecurityException and pass the old exception to the new one to chain them together .

try {
    ...
} catch (FileNotFoundException e) {
    throw new CustomEnterpriseSecurityException(e);
}

By chaining the exceptions you won't lose the stack trace from the original exception.

May be you can do like this:

try{
// file operations
}catch(FileNotFoundException e){
 throw new MyCustomeFileNotFoundException(e);
}

Create your exception version for predefined exceptions and when you get any predefined exception in catch throw your defined exception.

You can do this. Just catch the original exception and throw an appropriate exception you defined yourself and which extends EnterpriseSecurityException. Ensure that you chain the exceptions so that the stacktrace does not get lost.

For example with your file open example:

 try {
      //open file
    } catch (FileNotFoundException e) {
       throw new YoureCustomException("This is your message", e);
    }

A technique I've found very useful is to chain exceptions by using the Exception(Throwable cause) constructor as the superclass constructor. That way, you don't lose any of the stack trace, but can provide custom handling in your own class (which can look at the cause with the getCause() method if it wishes). For instance:

try{
    //do something
}
catch(FileNotFoundException e){
    throw new MyFileNotFoundException(e);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM