How to Handle a java.lang.ArithmeticException in Java? Last Updated : 21 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In Java programming, the java.lang.ArithmeticException is an unchecked exception of arithmetic operations. This means you try to divisible by zero, which raises the runtime error. This error can be handled with the ArthmeticException. ArithmeticException can be defined as a runtime exception that can occur during an arithmetic operation due to the exceptional condition. ArithmeticException is part of the java.lang package. In this article, we will learn how to handle a java.lang.ArithmeticException in Java. Syntax:try { // Code that may throw an ArithmeticExceptionExample: division by zero, } catch (ArithmeticException e) { // Exception handling code // Handle the ArithmeticException hereThis block is executed when an ArithmeticException is caughtExample: displaying an error message. } finally { // Optional block}Program to Handle a java.lang.ArithmeticException in JavaLet us understand programmatically, to handle a java.lang.ArithmeticException. Java // Java program to handle a java.lang.ArithmeticException public class GfGArithmeticException { // main method public static void main(String[] args) { int dividend = 10; int divisor = 0; try { // attempting division by zero int result = dividend / divisor; // print the result System.out.println("Result: " + result); } catch (ArithmeticException e) { // print the error System.out.println("ArithmeticException occurred: " + e.getMessage()); } finally { System.out.println("This is the example of the ArithmeticException"); } } } OutputArithmeticException occurred: / by zero This is the example of the ArithmeticException Explanation of the above Program:The above Java program is an example of the ArithmeticException. When we try to attempt dividing by zero, then try block raises the exception during the execution of the program.Then catch can take control and handle the exception and print the error of which type Exception can occur in the Java program. Create Quiz Comment S seepanarajvskq Follow 0 Improve S seepanarajvskq Follow 0 Improve Article Tags : Java Java Programs Java-Exceptions Java-Exception Handling Java Examples +1 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like