Reputation: 2707
I have UserDoesNotHaveApiKey exception with the following code:
public class UserDoesNotHaveApiKeyException extends Exception {}
Now on Splash screen, I do some check if a user has an API key or not and I want to throw UserDoesNotHaveApiException inside onError and open login screen.
As you can inside onError I have if statement and it always goes to else, even it is obvious that I have a correct exception.
Why?
Upvotes: 1
Views: 294
Reputation: 1835
Long story short, RuntimeException is different than other exceptions.
You could throw an Exception, an appropriate existing subclass of it (except RuntimeException
and its subclasses which are unchecked), or a custom subclass of Exception
.
In Java, there are two types of exceptions – checked and unchecked exception. Here’s the summary:
- Checked – Extends
java.lang.Exception
, for recoverable condition, try-catch the exception explicitly, compile error.- Unchecked – Extends
java.lang.RuntimeException
, for unrecoverable condition, like programming errors, no need try-catch, runtime error.
Upvotes: 1