DEV Community

Divya Divya
Divya Divya

Posted on

Exception in java

  • Exception is an Even.

  • Exceptions are used to detect and handle errors during program execution so the program can continue safely.

Example

package practice;

public class Home {
     static int a=10;
    static int b=0;

    public static void main(String[] args) {
        System.out.println(a/b);
    }
}

Enter fullscreen mode Exit fullscreen mode

Output

  • Variable a is store value
  • Variable b is store value and then java tries to calculate.
  • but, 10/0 division by zero is impossible because 10/0 is Infinity. So, Java throw an exception.

Why Exception are used?
Exceptions are used in Java to handle errors safely without stopping the entire program suddenly.

  • Prevent program crashes
  • Handle runtime errors safely
  • Maintain normal program flow
  • Show meaningful error messages

Two types of Exception

  1. Checked Exception
  2. Unchecked Exception

Top comments (0)