Exception Handling Part 01

Exception Handling Part 01

Introduction

  1. Compile Time error occurs when we have a syntactical error

  2. A runtime error occurs when we get at runtime after the code is successfully compiled this is called also called an exception

  3. 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

  1. We can handle runtime exceptions using try & catch block

  2. The try block will try or will check if the program has the error

  3. If no exception occurred in the try block then we will not get any error

  4. 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

  5. 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");
        }


    }
}