Java – Introduction to Exceptions

Introduction: While Compiling and Executing Java application, chance of getting 3 types of errors.

  • Compile time errors: Compiler raises error when we are not following language rules to develop the code.
    • Every Method should have return type.
    • Statement ends with semi-colon;
    • Invoking variable when it is not present
  • Logical Errors: If we get the output of code instead of Expected contains Logical error.
Expected			Result
1				12345
12				1234
123				123
1234				12
12345				1
  • Runtime Errors: Runtime Error is called Exception which terminates the normal flow of program execution.
    • Handling invalid input by user
    • Opening a file which is not present.
    • Connect to database with invalid user name and password.

InputMismatchException: It occurs if user enter invalid input while reading through Scanner.

import java.util.Scanner;
class Code
{
            public static void main(String[] args) {
                        Scanner sc = new Scanner(System.in);
                        System.out.println(“Enter 2 numbers : “);
                        int a = sc.nextInt();
                        int b = sc.nextInt();
                        int c = a+b;
                        System.out.println(“Sum is : ” + c);
            }
}

Handling Exception using try-catch:

try block:

  • It is used to place the code that may raise exception.
  • When error occurs an exception object will be raised.

catch block:

  • Exception object raised in try block can be collected in catch block to handle.
import java.util.Scanner;
import java.util.InputMismatchException;
class Code
{
            public static void main(String[] args) {
                        Scanner sc = new Scanner(System.in);
                        try{
                                    System.out.println(“Enter 2 numbers : “);
                                    int a = sc.nextInt();
                                    int b = sc.nextInt();
                                    int c = a+b;
                                    System.out.println(“Sum is : ” + c);
                        }
                        catch (InputMismatchException e){
                                    System.out.println(“Exception : Invalid input given”);
                        }
            }
}
Note : Catch block executes only if exception raises in try block.

ArrayIndexOutOfBoundsException: Occurs when we try to access array elements out of index.

void main()
{
            int[] arr = {10,20,30,40,50};
            System.out.println(arr[5]);
}

NumberFormatException: Occurs when we try to perform invalid data conversions.

void main()
{
            String s = “abc”;
            int x = Integer.parseInt(s);
}

ArithmeticException: Occurs when try to divide any number with zero.

 
void main()
{
            int a=10, b=0;
            int c=a/b;
}

NullPointerException: Occurs when we access variable or method .

void main()
{
            Stirng s = null;
            System.out.println(s.length());
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top