Launch custom exceptions in Java / Android
source link: https://www.codesd.com/item/launch-custom-exceptions-in-java-android.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Launch custom exceptions in Java / Android
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?
In addition to Eran's answer, you could also make your custom Exception extend RuntimeException
, which does not need to be caught.
Related Articles
How to create custom exceptions in Java?
Custom exception in java: 'can not find the symbol' if it is not explicitly imported
How to write custom exceptions for java connection
Launch custom exceptions, if for the loop
Clarification on how to launch and capture custom exceptions in Java
How to define my own message in my Custom Exception in java that can be reteived my getMessage () BUT WITHOUT using the constructor, is it possible?
Saving and retrieving a list of custom objects in Java / Android
Launch an exception in Java
The best way to write a custom exception name in Java
Custom Exception-Java
Kotlin - Launch a custom exception
Java setting custom exceptions inside the class, is this bad?
C # - Is it better to build a logic around launching a custom exception or business management?
No launcher activity found in eclipse java android
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK