简体   繁体   English

如何在java中传播异常

[英]How to propagate an exception in java

I am a C programmer and just learning some java recently because I am developing one android application.我是一名 C 程序员,最近刚学习了一些 Java,因为我正在开发一个 android 应用程序。 Currently I am in a situation.目前我处于一种情况。 Following is the one.下面是一个。

public Class ClassA{

public ClassA();

public void MyMethod(){

   try{
   //Some code here which can throw exceptions
   }
   catch(ExceptionType1 Excp1){
   //Here I want to show one alert Dialog box for the exception occured for the user.
   //but I am not able to show dialog in this context. So I want to propagate it
   //to the caller of this method.
   }
   catch(ExceptionType2 Excp2){
   //Here I want to show one alert Dialog box for the exception occured for the user.
   //but I am not able to show dialog in this context. So I want to propagate it
   //to the  caller of this method.
   }
   }
}

Now I wan to use call the method MyMethod() somewhere else in another class.现在我想在另一个类的其他地方使用调用方法 MyMethod() 。 If some one can provide me some code snippet how to propagate the exceptions to the caller of MyMethod() so that I can display them in a dialog box in the caller method.如果有人可以向我提供一些代码片段,如何将异常传播到 MyMethod() 的调用方,以便我可以在调用方方法的对话框中显示它们。

Sorry If I am not so much clear and weird in the way of asking this question.对不起,如果我问这个问题的方式不是那么清楚和奇怪。

Just don't catch the exception in the first place, and change your method declaration so that it can propagate them:首先不要捕获异常,并更改您的方法声明,以便它可以传播它们:

public void myMethod() throws ExceptionType1, ExceptionType2 {
    // Some code here which can throw exceptions
}

If you need to take some action and then propagate, you can rethrow it:如果你需要采取一些行动然后传播,你可以重新抛出它:

public void myMethod() throws ExceptionType1, ExceptionType2 {
    try {
        // Some code here which can throw exceptions
    } catch (ExceptionType1 e) {
        log(e);
        throw e;
    }
}

Here ExceptionType2 isn't caught at all - it'll just propagate up automatically.这里ExceptionType2根本没有被捕获 - 它只会自动传播。 ExceptionType1 is caught, logged, and then rethrown. ExceptionType1被捕获、记录,然后重新抛出。

It's not a good idea to have catch blocks which just rethrow an exception - unless there's some subtle reason (eg to prevent a more general catch block from handling it) you should normally just remove the catch block instead.不是一个好主意,有catch块刚刚重新抛出异常-除非有一些微妙的原因(例如,以防止处理它更一般的catch块)通常应该只是删除catch块来代替。

Don't catch it and rethrow again.不要抓住它并再次重新抛出。 Just do this and catch it in the place you want只需这样做并在您想要的地方抓住它

public void myMethod() throws ExceptionType1, ExceptionType2 {
    // other code
}

Example例子

public void someMethod() {
    try {
        myMethod();
    } catch (ExceptionType1 ex) {
        // show your dialog
    } catch (ExceptionType2 ex) {
        // show your dialog
    }
}

I always do it like this :我总是这样做:

public void MyMethod() throws Exception
{
    //code here
    if(something is wrong)
        throw new Exception("Something wrong");
}

then when you call the function然后当你调用函数时

try{
    MyMethod();
   }catch(Exception e){
    System.out.println(e.getMessage());
}

Just rethrow the exception只需重新抛出异常

throw Excp1;

You will need to add the exception type to the MyMthod() declaration like this您需要像这样将异常类型添加到MyMthod()声明中

public void MyMethod() throws ExceptionType1, ExceptionType2  {

   try{
   //Some code here which can throw exceptions
   }
   catch(ExceptionType1 Excp1){
       throw Excp1;
   }
   catch(ExceptionType2 Excp2){
       throw Excp2;
   }
}

Or just omit the try at all since you are no longer handling the exceptions, unless you put some extra code in the catch statements doing things with the exception before rethrowing it.或者完全省略try ,因为您不再处理异常,除非您在重新抛出异常之前在 catch 语句中放入一些额外的代码来处理异常。

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

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