Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Java Exception Handling

An exception is an unexpected or unwanted event that disrupt the normal flow of execution.

Errors:

Types Of Errors

In general there are three types of errors in java program such as

  • • Compile-time error
  • • Run-time error
  • • Logical error

Compile-Time Error

When the errors arise at the time of compilation known as compile time errors. Basically these errors are syntactical errors in a program that means the user is missing some syntax at the time of writing program. Consider the following example

Example
class Test 
{
	public static void main(String args[])
	{
		System.out.println("Silan Software")
	}
}

Output
';' expected
System.out.println("Silan Software")

In this program we are not writing a semicolon at the end of the statement. This syntactical error will be detected by java compiler.

Run-Time Error

The error arises at the time of execution is known as run-time error. That means sometimes a program may compile successfully but may not execute properly that means error is generating at the time of execution. Such errors are called run-time error. For example;

Example:
class Test 
{
	public static void main()
	{
		System.out.println("Silan Software");
	}
}

Output
exception in thread "main" java.lang. noSuchMethodError:main
Runtime errors are detected by JVM.

In the above program JVM cannot execute because it cannot understand the main() method without string args[].

Logical Error

These errors are generating by the poor understanding of a program. These errors are detected either by java compiler or JVM. For example, suppose we will write a program to find the area of a square.

Example:

class Square 
{
	public static void main( string args[ ])
	{
		int a=4;
		int ar;
		ar= a+a;
		system.out.println ("the area is "+ar);
	}
}

Output
the area is 8

We know that the area of a square is a*a but we have written a+a. So we will get wrong result that is 8. This is called as logical error.

Introduction to Exception
  • • An exception is nothing but a run-time error.
  • • An exception does not always indicate an error. It indicates an abnormal condition. For example dividing an integer by zero, accessing an element that is out of bound of an array, etc.
  • • Every program have a normal flow of execution. If an exception arises on which statement in a program then it will disrupt the flow of execution, that means the remaining statement can't execute.
  • • In java it is an object which is thrown at runtime

Let's see a program to represent an exception.

Example:
class ExceptionDemo
{
	public static void main(String args[])
	{
		int a[]={5,10,15,20,25};
		System.out.println(a[5]/2);
		//ArrayIndexOutOfBoundsException System.out.println("JavaRace");
		System.out.println("Welcome to JavaRace");
	}
}

Output

The above program is syntactically correct so it will compiled successfully but when it will execute , it displays the following message and stops without executing further statement.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:5 at ExceptionDemo.main <ExceptionDemo.java:6>

When JVM will try to execute to access the 5th position data item, it generates an error condition. Then the remaining statement can't execute. As a result program will stop.
Here we observed that an exception is a condition which is caused by a run-time error in the program. When JVM treats an error, it creates an exception object.

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext