Table of contents
Introduction
Compile Time error occurs when we have a syntactical error
A runtime error occurs when we get at runtime after the code is successfully compiled this is called also called an exception
This basically implies that the byte code of the program is generated correctly but when the JVM starts working on it, at that time this error occurs
Handling Runtime Error
We can handle runtime exceptions using try & catch block
The try block will try or will check if the program has the error
If no exception occurred in the try block then we will not get any error
But if an exception occurs then the lines written below in the try block will not get executed where get an exception, and then we will directly jump into the catch block
If we get an exception in the catch block then we will get an exception
example:
import java.util.Scanner;
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
try{
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
System.out.println(i);
sc.close();
}catch(Exception e){
System.out.println("enter the corect input");
}
}
}