Throw custom Exceptions in Java / Android

I am 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 am creating 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 !! This is not an idea, is it? I want to throw custom exceptions when I need them to be thrown without discarding my new Exceptions with more try/catch clauses.

So what am I doing wrong? What do I misunderstand?

+6
source share
5 answers

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

+13
source

If the method that throws this exception does not handle it (i.e., it does not catch it), it should declare it in the throws , since this is a checked exception.

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

if your custom exception is thrown from the Exception class, it must be handled (using try-catch ) or passed to the caller (using throws ). If you just want to leave it at runtime to handle the exception, you need to extend it from the RuntimeException Class. Since this is the first case in your script, 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 that he will now be responsible for the call with a try-catch or pass it again.

so you need to change the call statement as

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

You must declare your methods as throws UserException - or make your RuntimeException .

A later version is officially declared, but is often used to bypass the java mechanism of the declared exception .

+3
source

In Java, when you throw Exception , you need to do something else:

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

2. Or add a throws statement to the method definition, passing responsibility for handling Exception higher-level method.

This is part of the general OOP paradigm of modularity and abstraction: who is responsible for handling the Exception , the calling method, or the method itself? The answer depends on the nature of the Exception .

+2
source

Source: https://habr.com/ru/post/982932/


All Articles