Try/Catch ArithmeticException Tutorial

The ArithmeticException class is commonly thrown when the result of a method in the Math class is too large to fit into the resulting data type. Another common reason is a divide by zero exception. The ArithmeticException class is a subclass of RuntimeException; the RuntimeException class is an important class to mention because all subclasses that are derived from RuntimeException (and Error) are referred to as unchecked exceptions. I will be discussing the topic of unchecked versus checked exceptions in a future tutorial.

This tutorial builds on concepts from the following tutorials:



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md TryCatchAE
C:\Java>cd TryCatchAE
C:\Java\TryCatchAE>Notepad TryCatchAE.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class TryCatchAE {
    public static void main(String args[]) {
        int x = 0, y = 12, z = 0;

        try {
            x = y / z;
        } catch (ArithmeticException e) {
            System.out.println("Caught an arithmetic exception!");
        }
    
        z = Integer.MAX_VALUE + 5; // no exception, integer overflow ... data lost.
        System.out.println(z);
    
        try {
            z = Math.addExact(Integer.MAX_VALUE, 5);
        } catch (ArithmeticException e) { // try catching superclasses
            System.out.println("Caught an integer overflow exception: " + Integer.MAX_VALUE + " + " + 5);
            System.out.println("What can we do with the reference variable e?");
            System.out.println("getLocalizedMessage() = " + e.getLocalizedMessage());
            System.out.println("getMessage() = " + e.getMessage());
            System.out.println("toString() = " + e.toString());
            e.printStackTrace();
        }

        System.out.println("\n---------\n");
        try {
            z = Math.addExact(Integer.MAX_VALUE, y / x); // change x to 1
        } catch (ArithmeticException e ) {
            System.out.println(e.getMessage());
        }

        System.out.println("\nGood bye");
    }
}

Now switch back to the command prompt (CMD) and type in javac TryCatchAE.java and press Enter.
Now type in java TryCatchAE and press Enter.


C:\Java\TryCatchAE>javac TryCatchAE.java
C:\Java\TryCatchAE>java TryCatchAE
Caugth a divice by zeo exception: 12 / 0;
-2747483644
Caught an integer overflow exception: 21474836647 + 5
What can we do with the reference variable e?
getLocalizedMessage() = integer overflow
getMessage() = integer overflow
toString = java.lang.ArithmeticException: integer overflow
java.lang.ArithmeticException: integer overflow
    at java.lang.Math.addExact(Unknown Source)
    at TryCatchAE.main(TryCatchAE.java:15)


Final thoughts

It is worth mentioning again that ArithmeticException is derived from RuntimeException which makes it an unchecked exception.


Tutorials