简体   繁体   English

如何访问从另一个类引发的异常

[英]How can I access the exception raised from another class

My requirement is to send automatic mail whenever the exception arises 我的要求是在出现异常时发送自动邮件

public Connection dbConnect(String dbconnectionstring,String userid,String userpassword)
{
    Connection connection = null;
    System.out.println();
  try {
     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
     connection = DriverManager.getConnection(dbconnectionstring,userid,userpassword);
     System.out.println("connected to" + dbconnectionstring );

    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

  return connection;

now in the other class, if I want to access the content of Exception e (if any exception raises), what should be written in my code to know if any exception raised? 现在在另一个类中,如果我想访问Exception e的内容(如果有任何异常引发),应该在我的代码中写什么来知道是否引发了任何异常? and if the exception raised, the content of e should be mailed. 如果提出例外,应邮寄e的内容。

Re-throw the same exception, or wrap it inside an application-specific exception class (my preference): 重新抛出相同的异常,或将其包装在特定于应用程序的异常类(我的首选项)中:

    ...
} catch (InstantiationException e) {
    ...
    throw new MyAppException(e);
}

It depends what "other" is. 这取决于“其他”是什么。 If it is the caller of your doConnect() method just do not catch the exceptions but throw them and catch in caller method: 如果它是doConnect()方法的调用者,则不要捕获异常但抛出它们并捕获调用方法:

public void doConnect(....) throws InstantiationException, IllegalAccessException, .......{
}

public void theCaller() {
    try {
    doConnect(......)
    } catch(InstantiationException e) {
    }
    } catch(IllegalAccessException e) {
    }
/// etc
}

BTW in java 7 you can catch all exceptions in one block: 在java 7中BTW,您可以在一个块中捕获所有异常:

} catch(IllegalAccessException | InstantioationException | SqlException e)

Alternatively you can wrap specific exception using other exception that can be either checked or unchecked (runtime), eg 或者,您可以使用可以选中或取消选中的其他异常(运行时)来包装特定异常,例如

public Connection dbConnect(String dbconnectionstring,String userid,String userpassword)
{
    Connection connection = null;
    System.out.println();
  try {
     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
     connection = DriverManager.getConnection(dbconnectionstring,userid,userpassword);
     System.out.println("connected to" + dbconnectionstring );

    } catch (InstantiationException | IllegalAccessException  | ClassNotFoundException  | SQLException e) {
        throw new IllegalStateException("Connection failure", e);
    }

  return connection;
}

Now the caller may catch IllegalStateException and extract its cause. 现在调用者可能会捕获IllegalStateException并提取其原因。

If however you need some kind of asynchronous listener that manages all exceptions you can implement one class like ExceptionManager that is being updated by other classes when exception happens there. 但是,如果您需要某种管理所有异常的异步监听器,您可以实现一个类,如ExceptionManager ,当异常发生时,其他类正在更新。 The ExceptionManager may be singleton, so the code will look like this: ExceptionManager可能是单例,因此代码如下所示:

public void doConnect() {
    try {
       doConnect(......)
    catch(IllegalAccessException | InstantiationException | SqlException e)
        ExceptionManager.getInstance().exceptionThrown(e);
    }
}

You can design it like a log manager: get the application-level instance of your report class, and in the exception handling, make it process the exception. 您可以像日志管理器一样设计它:获取报表类的应用程序级实例,并在异常处理中使其处理异常。

Or you can rethrow the exception and catch it at the highest level of the application, where it can handle any unexpected exception (used in Swing applications to avoid closing on a simple NPE). 或者您可以重新抛出异常并在应用程序的最高级别捕获它,它可以处理任何意外的异常(在Swing应用程序中使用以避免关闭简单的NPE)。

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

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