Showing posts with label Examples of exceptions. Show all posts
Showing posts with label Examples of exceptions. Show all posts

Wednesday 3 August 2022

What is an Exception?

An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.

Many different kinds of errors can cause exceptions: problems ranging from serious hardware errors, such as a hard disk crash, to simple programming errors, such as trying to access an out-of-bounds array element. When such an error occurs within a Java method, the method creates an exception object and hands it off to the runtime system. The exception object contains information about the exception including its type and the state of the program when the error occurred. The runtime system is then responsible for finding some code to handle the error. In terminology, creating an exception object and handing it to the runtime system is called throwing an exception.

Types of exceptions :

1. Checked exceptions
2. Unchecked exceptions

Examples of exceptions : 

The following are examples of exceptions:

1. ClassNotFoundException
2. IllegalStateException
3. IllegalArgumentException
4. NullPointerException
5. SQLException

Types of Exceptions

Exception handling is the process of responding to unwanted or unexpected events when a computer program runs. Exception handling deals with these events to avoid the program or system crashing, and without this process, exceptions would disrupt the normal operation of a program.

Types of exceptions :

Exceptions can come in the following two exception classes:

1. Checked exceptions

Also called compile-time exceptions, the compiler checks these exceptions during the compilation process to confirm if the exception is being handled by the programmer. If not, then a compilation error displays on the system. Checked exceptions include SQLException and ClassNotFoundException.

2. Unchecked exceptions

Also called runtime exceptions, these exceptions occur during program execution. These exceptions are not checked at compile time, so the programmer is responsible for handling these exceptions. Unchecked exceptions do not give compilation errors. Examples of unchecked exceptions include NullPointerException and IllegalArgumentException.

Examples of exceptions

The following are examples of exceptions:

SQLException: is a checked exception that occurs while executing queries on a database for Structured Query Language syntax.

ClassNotFoundException: is a checked exception that occurs when the required class is not found -- either due to a command-line error, a missing CLASS file, or an issue with the classpath.

IllegalStateException: is an unchecked exception that occurs when an environment's state does not match the operation being executed.

IllegalArgumentException: is an unchecked exception that occurs when an incorrect argument is passed to a method.

NullPointerException: is an unchecked exception that occurs when a user tries to access an object using a reference variable that is null or empty.