How to create Checked/Unchecked custom exceptions in Java?


  1. Checked Exceptions
  2. 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

    public class EmailException extends Exception { public EmailException(String message) { super(message); } }

    public class Registration { List<String> registeredEmails = Arrays.asList("", ""); public void validEmail(String email) throws EmailException { if (registeredEmails.contains(email)) { throw new EmailException("Email Registered already in the system"); } } }

    public class Registrations { public static void main(String[] args) { Registration s = new Registration(); try { service.validateEmail(""); } catch (EmailException e) { // logging and handling the situation } } }


    mynotes.custom.checked.exception.EmailException: Email Registered already in the system at mynotes.custom.checked.exception.Registration.validateEmail( at mynotes.custom.checked.exception.Registrations.main(

    In this code we have create our own checked exception having name EmailException.

  3. Unchecked Exceptions
  4. 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.

    public class DomainException extends RuntimeException { public DomainException(String message) { super(message); } }

    public class Registration { public void validateEmail(String email) { if (!isDomainValid(email)) { throw new DomainException("Invalid"); } } private boolean isDomainValid(String email) { List<String> validDomains = Arrays.asList("", "", ""); if (validDomains.contains(email.substring(email.indexOf("@") + 1))) { return true; } return false; } }

    public class RegistrationClient { public static void main(String[] args) { Registration service = new Registration(); service.validateEmail(""); } }


    Exception in thread "main" mynotes.custom.unchecked.exception.DomainNotValidException: Invalid at mynotes.custom.unchecked.exception.Registration.validateEmail( at mynotes.custom.unchecked.exception.RegistrationClient.main(

    In this code we have create our exception having name DomainException and extends it with the RuntimeException.


Hopefully this article help you to learn about how to create custom checked and unchecked exception.


Clear Any Java Interview by Reading our Ebook Once.

Can you clear Java Interview?



Hot Deals ends in

Technical Quizzes Specially For You:

Search Tags

    Create Custom Checked Exception in Java

    Create Custom Runtime Exception in Java

    Business Exception Class Example