Exception Handling – Divide by Zero – Java Example

By | June 25, 2011

Hi,

Given below is a code snippet which will demonstrate an exception handling in Java with Divide by zero error.

public class MainClass {
  public static void main(String args[]) {

    int urAns, urDiv;

    try {

      urDiv = 0;
      urAns = 25 / urDiv;

      System.out.println("Do you really think this will print out? No! It won't!");

    }

    catch (ArithmeticException e) {

      System.out.println("Division by zero not Possible!");

    }

    System.out.println("This will print out after Exception Handling");
  }

}

🙂

Leave a Reply

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