Throw custom Exceptions in Java/Android

旧巷老猫 提交于 2019-12-19 12:36:11

问题


I'm developing a custom Exception class, in which I have the following constructors:

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!! 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.

So, what I'm doing wrong? What I'm misunderstanding?


回答1:


In addition to Eran's answer, you could also make your custom Exception extend RuntimeException, which does not need to be caught.




回答2:


If the method that throws this exception doesn't handle it (i.e. it doesn't catch it), it must declare it in the throws clause, since this is a checked exception.

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



回答3:


if your Custom Exception extends from Exception class, it must be handled (using try-catch) or passed on to caller (using 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:

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.

so again, u need to modify calling instruction as

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



回答4:


You should either declare your methods as throws UserException - or make your exception extend RuntimeException.

The later is officially unadvised, but is often used to bypass the java's declared exception mechanism.




回答5:


In Java, when you throw a checked Exception, there is one more thing you are required to do:

1. Either add a try-catch block around the throw and handle this Exception within the same method.

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.

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 ? The answer depends on the nature of the Exception.



来源:https://stackoverflow.com/questions/28693363/throw-custom-exceptions-in-java-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!