Create Custom Exception in Java

Exercise:

Write a Java Program to create custom exception.

1.create custom checked exception

Click Here to View the Solution!
import java.util.ArrayList;
import java.util.Arrays;

    // create a checked exception class
    class CustomException extends Exception {
    public CustomException(String message) {
        super(message);
    }
}
public class Main {
    ArrayList<String> cities = new ArrayList<>(Arrays.asList("London", "New York", "Washington"));

    // check the exception condition
    public void checkCity(String city) throws CustomException {

        // throw exception if city already present in ArrayList
        if(cities.contains(city)) {
            throw new CustomException(city + " already exists");
        }
        else {

            // insert city to ArrayList
            cities.add(city);
            System.out.println(city + " is added to the ArrayList");
        }
    }
    public static void main(String[] args) {

        // create object of Main class
        Main obj = new Main();

        // exception is handled using try...catch
        try {
            obj.checkCity("Delhi");
            obj.checkCity("London");
        }
        catch(CustomException e) {
            System.out.println("[" + e + "] Exception Occured");
        }
    }
}
Click Here to View the Output!
Delhi is added to the ArrayList
[CustomException: London already exists] Exception Occured
Click Here to View the Explanation!
  • This program deals with creating customized case of checked exceptions.
  • These types of exceptions are ought to be dealt with either a try-catch block in the program, or utilize throws keyword.
  • In other words compiler will throw an error.
  • To create custom exception, we create a class CustomException that extends its parent Exception class.
  • In its constructor, we call super class constructor i.e. Exception.
  • After that, we create a Main class which at first declares a new String type ArrayList by the name of cities.
  • Main class contains a method checkCity(), having a string type parameter, that throws an exception if the value in the argument is already present in the city list, otherwise that value is appended to the list.
  • The try-catch block in the main method, try calls the checkCity() method, and catch handles the exception that occurs during the second call with Java in the argument.   

2.custom unchecked exception class

Click Here to View the Solution!
import java.util.ArrayList;
import java.util.Arrays;

// create a unchecked exception class
class CustomException extends RuntimeException {
    public CustomException(String message) {
        // call the constructor of RuntimeException
        super(message);
    }
}
public class Main {
    ArrayList<String> cities = new ArrayList<>(Arrays.asList("London", "New York", "Washington"));

    // check the exception condition
    public void checkCity(String city) {

        // throw exception if city already present in ArrayList
        if(cities.contains(city)) {
            throw new CustomException(city + " already exists");
        }
        else {
            // insert city to ArrayList
            cities.add(city);
            System.out.println(city + " is added to the ArrayList");
        }
   }
    public static void main(String[] args) {
    
        // create object of Main class
        Main obj = new Main();

        // check if city already present
        obj.checkCity("Delhi");
        obj.checkCity("London");
    }
}
Click Here to View the Output!
Delhi is added to the ArrayList
Exception in thread "main" CustomException: London already exists
     at Main.checkCity(Main.java:21)
     at Main.main(Main.java:37)
Click Here to View the Explanation!
  • This program deals with creating customized case of unchecked exceptions.
  • These types of exceptions are thrown when the program is executed, and are not checked during compile time.
  • Therefore no try-catch block is required.
  • The procedure for custom unchecked exception is similar to the previous example with slight changes.
  • The foremost step is the definition of CustomException class which extends RunTimeException class.
  • Similar to the previous case, we call the constructor of super class.
  • Again we define a Main class, with a function that’ll throw an exception.
  • If the value in the argument does not exist in the ArrayList, then it is appended to the list.
  • Otherwise, a message (exception) is displayed indicating that it already exists.
%d bloggers like this: