DEV Community

VIDHYA VARSHINI
VIDHYA VARSHINI

Posted on

Exception Handling in Java

What is exception handling: The term "exception" is simply known as "exceptional event", which means it occurs during the execution of a program, that disrupts the normal flow of the program's instructions.

  1. When an error occurs within a method, the method creates an object and hands it off to the runtime system.
  2. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred.
  3. Creating an exception object and handing it to the runtime system is called throwing an exception.
  4. After a method throws an exception, the runtime system attempts to find something to handle it.
  5. The set of possible "somethings" to handle the exception is the ordered list of methods that had been called to get to the method where the the error occurred. The list of methods is known as the "call stack".


package exceptionhandling;

public class Arithmeticexception {

    public static void add(int a, int b) {
        System.out.println(a+b);
    }

    public static void sub(int a, int b) {
        System.out.println(a-b);
    }

    public static void divide(int a, int b) {
        System.out.println(a/b);
    }



    public static void main(String[] args) {
     int a = 10;
     int b = 5;
     add(a,b);
     sub( a, b);
     divide( a, b);

Enter fullscreen mode Exit fullscreen mode

Output:

15
5
2
Enter fullscreen mode Exit fullscreen mode

Top comments (0)