ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Checked and Unchecked Exceptions
    StaticPL/JAVA 2019. 8. 23. 17:30

    1. Overview

    Let's distribute Java exceptions into two main categories: checked exceptions and unchecked exceptions. 

    2. Description

    2.1 Checked Exceptions

    • Represent errors outside the control of the program.
    • Such as IOException, SQLException, and ParseException.
    • Exception is the superclass of these Exceptions

    The constructor of FileInputStream throws FileNotFoundException if the input file does not exist.

    private static void checkedExceptionWithThrows() throws FileNotFoundException {
        File file = new File("not_existing_file.txt");
        FileInputStream stream = new FileInputStream(file);
    }

    Also using a try-catch block to handle a checked exception available.

    private static void checkedExceptionWithTryCatch() {
        File file = new File("not_existing_file.txt");
        try {
            FileInputStream stream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    The Exception class is the superclass of checked exceptions. Therefore, Creating a custom checked exception needs to extend Exception.

    public class IncorrectFileNameException extends Exception {
        public IncorrectFileNameException(String errorMessage) {
            super(errorMessage);
        }
    }

    2.2 Unchecked Exceptions

    • Throwing Exceptions at runtime
    • Reflecting some error inside the program logic
    • Unable to verify unchecked exceptions and no error at compile-time 
    • Unable to declare unchecked exceptions in a method with the throws keyword
    • RuntimeException is superclass of these Exceptions
    • Such as NullPointerException
    public class NullOrEmptyException extends RuntimeException {
        public NullOrEmptyException(String errorMessage) {
            super(errorMessage);
        }
    }

    2.3 When to use Which exception

    If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.

    2.3.1 Checked Exception

    If user's input file name is invalid, It needs to throw a custom checked exception like below

    if (!isCorrectFileName(fileName)) {
        throw new IncorrectFileNameException("Incorrect filename : " + fileName );
    }
    

    2.3.2 Unchecked Exception

    If the input file name is null point or it is an empty string, it means we have some errors in the our logic. In this case, throwing an unchecked exception

    if (fileName == null || fileName.isEmpty())  {
        throw new NullOrEmptyException("The filename is null or empty.");
    }
    

    3. References

    https://www.baeldung.com/java-checked-unchecked-exceptions

     

    'StaticPL > JAVA' 카테고리의 다른 글

    Generics and Generic Methods  (0) 2019.08.27
    Reflection  (0) 2019.08.27
    Fork Join framework  (0) 2019.08.23
    Garbage Collection (GC)  (0) 2019.08.23
    JDK, JRI, JVM, and Classloader  (0) 2019.08.23

    댓글

Designed by Tistory.