- Checked Exceptions
Checked Exceptions occurs during compile time of the code. Such type of exceptions should be compulsorily handled in Code, else it will throw a Compile Time Exception. In Checked Exceptions, exception is either handled with in the body of the Method in catch block otherwise its thrown to the caller method so that caller method can handle the exception.
How to create Checked Exceptions
Let's consider we have to code for the system that validate the email that we enter in the system. As we need unique e mail to enter in the system. If existent mail is being enter in the system detect it and shows message.
Code
EmailException.java
public class EmailException extends Exception {
public EmailException(String message) {
super(message);
}
}
Registration.java
public class Registration {
List registeredEmails = Arrays.asList("ABC@gmail.com", "XYZ@gmail.com");
public void validEmail(String email) throws EmailException {
if (registeredEmails.contains(email)) {
throw new EmailException("Email Registered already in the system");
}
}
}
Registrations.java
public class Registrations {
public static void main(String[] args) {
Registration s = new Registration();
try {
service.validateEmail("ABC@gmail.com");
} catch (EmailException e) {
// logging and handling the situation
}
}
}
Output:
mynotes.custom.checked.exception.EmailException: Email Registered already in the system
at mynotes.custom.checked.exception.Registration.validateEmail(Registration.java:12)
at mynotes.custom.checked.exception.Registrations.main(Registrations.java:9)
In this code we have create our own checked exception having name EmailException.
- Unchecked Exceptions
Unchecked Exceptions are the exceptions that occur during Runtime. Hence these types of exception is also known as Runtime exception. Although the compiler wouldn't complain about such types of Exception, but its better to handle that in your code to avoid any Runtime issues in your Code. For example if we divide 7 by 0 the java shows Arithmetic exception.
How to Create Unchecked Exceptions
Let's consider we have to create API in which we can have to whether the Email have valid domain name or not.
DomainException.java
public class DomainException extends RuntimeException {
public DomainException(String message) {
super(message);
}
}
Registration.java
public class Registration {
public void validateEmail(String email) {
if (!isDomainValid(email)) {
throw new DomainException("Invalid");
}
}
private boolean isDomainValid(String email) {
List validDomains = Arrays.asList("gmail.com", "yahoo.com", "outlook.com");
if (validDomains.contains(email.substring(email.indexOf("@") + 1))) {
return true;
}
return false;
}
}
RegistrationClient.java
public class RegistrationClient {
public static void main(String[] args) {
Registration service = new Registration();
service.validateEmail("abc@gmail1.com");
}
}
Output:
Exception in thread "main" mynotes.custom.unchecked.exception.DomainNotValidException: Invalid
at mynotes.custom.unchecked.exception.Registration.validateEmail(Registration.java:10)
at mynotes.custom.unchecked.exception.RegistrationClient.main(RegistrationClient.java:7)
In this code we have create our exception having name DomainException and extends it with the RuntimeException.
Conclusion
Hopefully this article help you to learn about how to create custom checked and unchecked exception.