what is an exception in java

what is an exception in java

1 year ago 34
Nature

In Java, an exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. It is an unwanted or unexpected event that can happen during the execution of a program, i.e., at run time, that disrupts the normal flow of the programs instructions. When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception. After a method throws an exception, the runtime system attempts to find something to handle it. The set of possible "somethings" to handle the exception is the ordered list of methods that had been called to get to the method where the error occurred. The list of methods is known as the call stack.

Java provides a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc., known as Java Exception Handling. Exceptions in Java can arise from different kinds of situations such as wrong data entered by the user, hardware failure, network connection failure, or a database server that is down. Java creates an exception object when an error occurs while executing a statement. The exception object contains a lot of debugging information such as method hierarchy, line number where the exception occurred, and type of exception.

Java defines several types of exceptions that relate to its various class libraries. Exceptions can be categorized in two ways: Built-in Exceptions, which are the exceptions that are available in Java libraries, and User-Defined Exceptions, which are the exceptions defined by the user. Built-in exceptions are suitable to explain certain error situations. Checked exceptions are the exceptions that are checked (notified) by the compiler at compilation-time, and these are also called as compile-time exceptions. These exceptions cannot simply be ignored, and the programmer should take care of (handle) these exceptions.

In summary, an exception in Java is an event that occurs during the execution of a program that disrupts the normal flow of instructions. Java provides a mechanism to handle runtime errors known as Java Exception Handling. Exceptions can be categorized in two ways: Built-in Exceptions and User-Defined Exceptions.

Read Entire Article