Java J2ME JSP J2EE Servlet Android

Java by Example: Exception Handling – Java Exception Basics

All related Posts
Java by Example: Exception Handling, creating custom exception class
Java by Example: Exception handling – throw custom Exception
Java By Example: Exception Handling – Throwing exception from method
Java by Example: Exception handling – Basic Exception Handling example ‘try catch’
Java by Example: Exception Handling – Java Exception Basics

Java provides strong way to cope with Errors and Exceptions. When an illegal operation occurred Java throws exception. And When exception occurs program execution ends if exception is not handled properly. There is a class below to understand Java Exception..


 

public
class ExceptionExp1 {


 

    public
static
void main(String args[]){

        String strArray[] = {"This","is", "an", "example", "of", "Java","exception" };

        System.out.println(strArray[0]);

        System.out.println(strArray[1]);

        System.out.println(strArray[3]);

        System.out.println(strArray[4]);

        System.out.println(strArray[5]);

        System.out.println(strArray[6]);

        System.out.println(strArray[7]);

        System.out.println("Program will terminate before executing this line");

    }

}


 

If you execute this program, the output will be-

This

is

example

of

Java

exception

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7

    at example.java.lang.ExceptionExp1.main(ExceptionExp1.java:13)

Here exception occurs before ends the program and terminates.