Core Java

Exceptions in Java : An Introduction

Java Exception framework helps us to handle exceptions in Java very effectively and without much pain. In this post, we’ll learn about Exceptions in Java, their types, the difference between checked and unchecked exceptions. We’ll also cover significantly good number of classes available in Java Exception framework.

Introduction :

Exceptions are abnormal disruptions in the normal execution flow of a program mostly due to failing semantic requirements. For instance, intended file was not found in the expected location or the object reference is null. Java provides excellent support for handling exceptions.

Exceptions shouldn’t be confused with errors. Errors indicate that something severely wrong has happened. It usually leads to application being crashed as by no means it can be handled within our code. For example : StackOverFlowError.

Both Exception and Error are Java Objects and they extend from the java.lang.Throwable class.

Exception Classification in Java

Types of Exceptions in Java :

There are two broad categories of exceptions in Java:

  1. Checked Exceptions : These are the exceptions for which compiler warns us to either handle or declare them, or else the code compilation fails. All exceptions other than RuntimeException, Error  and their subclasses fall under this category. For example : SQLException, ClassNotFoundException, IOException etc.
  2. Unchecked Exceptions : As the name suggests, the compiler doesn’t warns the programmer to handle these types of exceptions. It is programmer’s responsibility to handle them appropriately and ensure a safe program termination. They are also known as Runtime exceptions. Some examples include :ArrayIndexOutOfBoundsException, ArithmeticException, NullPointerException etc.

Important Exception Framework Classes :

Let’s have a look at some of the most common exception classes that we might come across from time to time.

Nugget : Not all exception classes belong to java.lang language.

  • Throwable :  java.lang.Throwable class is the super class of all the exception/error related classes in the Java exception handling framework. All classes of throwables define a single parameter constructor which takes in a string argument referring to the exception message text. Below are the two commonly used methods available in Throwable class.
    //Returns the message text describing the exception
    String getMessage(){
    ............
    }
    
    //Prints the stack trace of the exception
    void printStackTrace(){
    ......
    }
    
    
  • Exception : It is a super class of all types of exceptions including both checked and unchecked/ runtime exceptions.
    • ClassNotFoundException : It is a checked exception which signals that the class loader could find the requested class to be loaded.
    • RuntimeException : java.lang.RuntimeException and all of its subclasses are unchecked/runtime exceptions which usually occur due to programming errors which should not occur in the first place.
    • ArithmeticException : It occurs when trying to perform some illegal arithmetic operation. DivideByZeroException is a subclass of ArithmeticException.
    • ArrayIndexOutOfBoundException : It occurs when trying to access an invalid index of an array i.e. index < 0 or index > = length of an array.
    • ClassCastException : It signals a cast among incompatible objects is attempted. For instance, trying to type cast Short object to Integer object etc.
    • IllegalArgumentException : It occurs when an illegal argument is supplied to a method. For eg : java.time.format.DateTimeFormatter.ofPattern(String pattern) methods throws this exception when supplied an invalid pattern.
    • NumberFormatException : It is a subclass of IllegalArgumentException. It is thrown when the conversion from string to a numeric value is not possible.
    • NullPointerException : Thrown when trying to access object members using a null reference.
  • Error : Error class along with its subclasses define irrecoverable conditions that cannot be handled in a program.
    • AssertionError : It is thrown by the JVM when assert() statement returns false.
    • StackOverFlowError : It occurs when the memory stack is full – mostly when there is an infinite loop or a very deep recursion.

Conclusion :

Today we covered what exceptions mean and how they are different from errors. We also looked at the types of exceptions – checked and unchecked exceptions.

We have looked at a significant number of classes from Java Exception framework. In the upcoming posts, we’ll learn how to handle exceptions in Java using try, catch and finally. Please feel free to check it out!

Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *