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);
}
}
Output
- Variable
ais store value - Variable
bis store value and then java tries to calculate. - but,
10/0division by zero is impossible because10/0is 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
- Checked Exception
- Unchecked Exception

Top comments (0)