Java 将异常抛回调用方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21028708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:03:35  来源:igfitidea点击:

Throwing Exception back to the calling method

javaandroidexception

提问by Boardy

I am working on an android project and I am trying to figure out how to throw an exception back to the calling thread.

我正在开发一个 android 项目,我想弄清楚如何将异常抛出回调用线程。

What I have is an activity, and when the user clicks on a button it calls a threaded function within another java class (not activity, standard class). The method within the standard class can throw an IOExceptionor Exception. I need to throw the exception object back to the calling method within the activity so that the activity can do some stuff based on what the exception was returned.

我拥有的是一个活动,当用户单击一个按钮时,它会调用另一个 Java 类(不是活动,标准类)中的线程函数。标准类中的方法可以抛出IOExceptionException。我需要将异常对象抛出回活动中的调用方法,以便活动可以根据返回的异常执行一些操作。

Below is my activity code:

以下是我的活动代码:

private void myActivityMethod()
{
    try
    {
        MyStandardClass myClass = new MyStandardClass();
        myClass.standardClassFunction();
    }
    catch (Exception ex)
    {
        Log.v(TAG, ex.toString());
        //Do some other stuff with the exception
    }
}

Below is my standard class function

下面是我的标准类函数

private void standardClassFunction()
{
    try
    {
        String temp = null;
        Log.v(TAG, temp.toString()); //This will throw the exception as its null
    }
    catch (Exception ex)
    {
        throw ex; //Don't handle the exception, throw the exception backto the calling method
    }
}

When I put throw exin the exception, Eclipse seems to be unhappy, and instead asks me to surround the throw exwithin another try/catch, which them means, if I do this, the exception is then handled within the second try/catch not the calling methods exception handler.

当我放入throw ex异常时,Eclipse 似乎不高兴,而是要求我将其包围throw ex在另一个 try/catch 中,这意味着,如果我这样做,则异常将在第二个 try/catch 中处理,而不是调用方法异常处理程序。

Thanks for any help you can provide.

感谢您的任何帮助,您可以提供。

采纳答案by vipul mittal

Change:

改变:

private void standardClassFunction()
{
    try
    {
        String temp = null;
        Log.v(TAG, temp.toString()); //This will throw the exception as its null
    }
    catch (Exception ex)
    {
        throw ex; //Don't handle the exception, throw the exception backto the calling method
    }
}

to

private void standardClassFunction() throws Exception 
{

        String temp = null;
        Log.v(TAG, temp.toString()); //This will throw the exception as its null

}

If you want to handle the exception thrown in called function inside calling function. You can do by just not catching it rather throwing it as above.

如果要处理调用函数内部被调用函数抛出的异常。你可以通过不抓住它而是像上面那样扔掉它来做到这一点。

Also if it is a checked exception like NullPointerException you don't even need to write throws.

此外,如果它是像 NullPointerException 这样的已检查异常,您甚至不需要编写 throws。

More about checked and unchecked exceptions:

有关已检查和未检查异常的更多信息:

http://www.geeksforgeeks.org/checked-vs-unchecked-exceptions-in-java/

http://www.geeksforgeeks.org/checked-vs-unchecked-exceptions-in-java/

回答by Tal87

as mentioned above, when you declare throws in the method signature, the compiler knows that this method may throw an exception.

如上所述,当您在方法签名中声明 throws 时,编译器知道此方法可能会抛出异常。

so now when you call the method from the other class, you'll be asked to surruond your call in try/catch.

所以现在当你从另一个类调用方法时,你会被要求在 try/catch 中调用你的调用。