简体   繁体   English

更改方法抛出Java的异常类型

[英]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. 我想做的事情感觉反直觉,但Java一直让我感到惊讶,所以我试一试。

I am implementing an application with the help of the ESAPI library. 我正在ESAPI库的帮助下实现一个应用程序。 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. 除了众所周知的异常行为之外,这些异常还会执行诸如使用log4j自动记录和向入侵检测模块提供信息之类的操作。

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. 无论何时我的方法抛出异常,我都不会抛出正常的异常,而是从新的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? 假设我想使用java.io.FileInputStream读取文件? It cannot find the file and throws a FileNotFoundException. 它找不到该文件并抛出FileNotFoundException。 Unfortunately the FileNotFoundException does not provide the features of my own Exceptions, because it does not extend the EnterpriseSecurityException. 不幸的是,FileNotFoundException不提供我自己的异常的功能,因为它不会扩展EnterpriseSecurityException。

Is there any trick in Java to change the exceptions a method throws? Java中是否有任何技巧可以更改方法抛出的异常? 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 . 捕获异常并抛出一个从EnterpriseSecurityException扩展的新异常,并将旧异常传递给新异常以将它们链接在一起

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. 为预定义的异常创建异常版本,并在catch中获得任何预定义的异常时抛出定义的异常。

You can do this. 你可以这样做。 Just catch the original exception and throw an appropriate exception you defined yourself and which extends EnterpriseSecurityException. 只需捕获原始异常并抛出您自己定义的适当异常,并扩展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. 我发现一种非常有用的技术是使用Exception(Throwable cause)构造函数作为超类构造函数来链接异常。 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). 这样,您不会丢失任何堆栈跟踪,但可以在您自己的类中提供自定义处理(如果愿意,可以使用getCause()方法查看原因)。 For instance: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过反射将子类类型传递给Java泛型方法不会引发异常 - Passing Subclass Type to Java Generic Method with reflection throws no Exception Java循环抛出异常的方法 - Java looping a method if it throws exception 引发异常的方法,该方法调用另一个引发不同异常的方法(java) - method that throws exception that calls another method that throws a different exception (java) 为什么java允许一个总是抛出异常的方法将返回类型声明为异常? - Why does java allow a method which always throws an exception to declare the return type as that exception? Java方法包含另一个引发异常的方法 - Java method contains another method that throws exception Java方法包含另一个抛出异常的方法 - Java method contains another method throws exception java - 在重写方法中检查'throws'的异常 - java - checked exception for 'throws' in overridden method Java getMethod抛出方法未找到异常? - Java getMethod throws method not found exception? Java 最终阻塞并在方法级别抛出异常 - Java finally block and throws exception at method level 如果我们在方法签名中抛出子类型异常,是否可以在方法中抛出Parent类型异常? - Can we throw Parent type exception in the method if we written throws child type exception in the method signature?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM