Introduction of Exception Handling
previous NextAn unwanted, unexpected event that interrupts the normal flow of the program is called an exception. Or unwanted termination of the program is called an exception.
Exception and Error in java
Exceptions and errors both are subclasses of the Throwable class. The error indicates a problem that mainly occurs due to the lack of system resources and our application should not catch these types of problems. Some examples of errors are system crash errors and memory errors. Errors mostly occur at runtime that's they belong to an unchecked type.
Exceptions are the problems that can occur at runtime and compile time. It mainly occurs in the code written by the developers. Exceptions are divided into two categories such as checked exceptions and unchecked exceptions.
Example for Errors in java
public class ErrorExample {
public static void main(String[] args){
recursiveMethod(10)
}
public static void recursiveMethod(int i){
while(i!=0){
i=i+1;
recursiveMethod(i);
}
}
}
Output :
Exception in thread "main" java.lang.StackOverflowError at ErrorExample.ErrorExample(Main.java:42)
Example for Exception in java
public class ExceptionExample {
public static void main(String[] args){
int x = 100;
int y = 0;
int z = x / y;
}
}
Output :
java.lang.ArithmeticException: / by zero at ExceptionExample.main(ExceptionExample.java:7)
![](https://www.learnhindituts.com/assets/images/learnhindituts-learn-tutorials-in-hindi.png)