简体   繁体   English

在Java / Android中抛出自定义异常

[英]Throw custom Exceptions in Java/Android

I'm developing a custom Exception class, in which I have the following constructors: 我正在开发一个自定义的Exception类,其中我有以下构造函数:

public class UserException extends Exception
{
    string message;
    public UserException() {
        super();
    }

    public UserException(String message, Throwable cause)
    {
        super(message, cause);

        this.cause = cause;
        this.message = message;
    }
}

And I create a new custom exception like this: 我创建了一个新的自定义异常,如下所示:

private Camera.PreviewCallback SetPreviewCallBack() throws UserException {
    .......
    // Something went wrong
    throw new UserException("Something failed.", new Throwable(String.valueOf(UserExceptionType.CaptureFailed)));
 }

But when I insert my throw new UserException(...) it tells me to surround it with try/catch !! 但当我插入我的throw new UserException(...)它告诉我用try/catch包围它! That's not the idea, isn't it? 这不是主意,不是吗? I want to throw custom exceptions when I need them to be thrown, without surronding my new Exceptions with more try/catch clauses. 我希望在需要抛出自定义异常时抛出自定义异常,而不会使用更多try/catch子句来覆盖我的new Exceptions

So, what I'm doing wrong? 那么,我做错了什么? What I'm misunderstanding? 我误解了什么?

除了Eran的答案之外,您还可以使自定义Exception扩展RuntimeException ,而不需要捕获它。

If the method that throws this exception doesn't handle it (ie it doesn't catch it), it must declare it in the throws clause, since this is a checked exception. 如果抛出此异常的方法不处理它(即它没有捕获它),它必须在throws子句中声明它,因为这是一个经过检查的异常。

public void yourMethod () throws UserException
{
    ...
    throw new UserException("Something failed.", new Throwable(String.valueOf(UserExceptionType.CaptureFailed)));
    ...
}

if your Custom Exception extends from Exception class, it must be handled (using try-catch ) or passed on to caller (using throws ). 如果您的自定义异常从Exception类扩展,则必须处理它(使用try-catch )或传递给调用者(使用throws )。 If you just want to leave it to runtime to handle the exception, You need to extend it from RuntimeException Class Since its the 1st case in your scenario, You should do something like this: 如果你只想将它留给运行时来处理异常,你需要从RuntimeException类扩展它因为它是你的场景中的第一种情况,你应该这样做:

public void surroundingMethod() throws UserException{
    throw new UserException("Something failed.", new Throwable(String.valueOf(UserExceptionType.CaptureFailed)));
}

this will essentially pass your exception to the caller, so now it will be caller's responsibility to handle it with try-catch or pass it on again. 这实际上会将您的异常传递给调用者,所以现在调用者有责任使用try-catch处理它或者再次传递它。

so again, u need to modify calling instruction as 所以,你需要修改调用指令

public void callingMethod () {
    try {
        surroundingMethod();
    } catch (UserException ex){
    }
}

You should either declare your methods as throws UserException - or make your exception extend RuntimeException . 您应该将您的方法声明为throws UserException - 或者使您的异常扩展RuntimeException

The later is officially unadvised, but is often used to bypass the java's declared exception mechanism . 后者是官方未经修改的,但通常用于绕过java声明的异常机制

In Java, when you throw a checked Exception , there is one more thing you are required to do: 在Java中,当您throw一个已检查的 Exception ,还需要做一件事:

1. Either add a try-catch block around the throw and handle this Exception within the same method. 1.throw周围添加一个try-catch块,并在同一个方法中处理此Exception

2. Or add a throws statement to the method definition, transferring the responsibility for the handling of the the Exception to a higher-level method. 2.或者向方法定义添加throws语句,将处理Exception的责任转移到更高级别的方法。

This is part of the overall OOP paradigms of modularity & abstraction: who is responsible for handling an Exception , the caller of a method or the method itself ? 这是模块化和抽象的整体OOP范例的一部分:谁负责处理Exception ,方法的调用者或方法本身? The answer depends on the nature of the Exception . 答案取决于Exception的性质。

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

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