Every programmer wants to make a complete program that can function under unexpected incidents and that is called exception handling. So far, we have discussed the theory of program structure development under specified programming languages. In this section, we'll discuss exception handling in Java through the use of try-catch blocks.
In this instance, Windows will temporarily halt the program attempt and notify us of the exception that was thrown. Without exception handling, the program might not be executed as planned and might be closed. Throughout this examination, we will discuss the details of exception handling in a Java environment, how try-catch blocks are created, and how to utilize them in writing better, more tolerant and safer programs.
So when an event does happen and the Exception occurs, the particular Java program's execution environment (JRE) creates an exception object which includes info about the problem that the program has encountered. The Java program is said to crash if such an abnormal condition is not handled. In simple terms, the Abnormal exit of the program is when the program terminates at any point when an exception arises. Exception handling takes care of such scenarios.
The following are the two primary exceptions categories that can be encountered in Java programming language:
- Checked exceptions : These are exceptions that are checked at compile time. They represent the exceptions, which a method has to catch or which are incorporated in the method signature using the throws clause. Some of them are : `IOException`, `SQLException`, etc.
- Unchecked exceptions : These are the exceptions that do not need to be handled and occur during the execution of the program. They are the logical errors in the code of a program, for instance, `NullPointerException`, `ArrayIndexOutOfBoundsException`, etc.
1. Try block : This is where the code that is likely to throw an exception is written. It is contained in a `try` statement.
2. Catch block : The system executes this block in case the code in the `try` block results in an error. It comes after the `try` statement and is preceded by the `catch` statement.
The raising of exceptions in the `try` block results in stopping the execution of all other remaining statements within that `try` block, and instead shifts the control to the corresponding `catch` block. The `catch` block then handles the exception, typically by logging the error, showing an error message to the user, or performing a corrective action.
- In case an exception occurs at any time during the execution of the code in the `try` block, control shifts to the relevant `catch` block.
- The `catch` block rescues the exception thrown and executes the appropriate code written within it. Next, the program picks up from the code placed after the `catch` block.
The type of exception the catch block will deal with can be stated inside the block. This provision enables one to handle different types of exceptions in a more specific and efficient way.
When several `catch` clauses are used, each `catch` clause can handle a single exception. So, it does not avoid `try` and `catch` when an exception is thrown, but rather, tries to find the appropriate `catch` statement for the exception. This feature helps to have a systematic way of handling the exceptions, as a particular `catch` is dealt with by letting other exceptions go.
But there is no `catch` without an exception. Exceptions are also handled in a nested manner. A more specific exception should be written before a more general exception. This is as a rule because when a `catch` block catches an exception no more blocks are run. If we have a specific block for one exception then definitely a general one must have been defined at a certain level of the hierarchy of exceptions, but that will be useless as the specific block would never be reached.
As we know, a `finally` block has no conditions; it will always execute whether or not an exception has been thrown. If an exception was not encountered at all, then the `finally` block is always executed after the `try` block. Therefore it is used to perform cleaning operations, such as freeing memory, closing database connections, and closing file streams.
The optional requirement of a 'finally' block should be emphasized. If no clean-up activity is required, it can be ignored. However, in the case of resources that need to be freed, a 'finally' block is essential.
Although nested try-catch blocks are convenient they should be employed with caution. This can cause the code to be convoluted and difficult to read so it is best to avoid this feature unless the situation absolutely requires it.
In this instance, Windows will temporarily halt the program attempt and notify us of the exception that was thrown. Without exception handling, the program might not be executed as planned and might be closed. Throughout this examination, we will discuss the details of exception handling in a Java environment, how try-catch blocks are created, and how to utilize them in writing better, more tolerant and safer programs.
1. Grasping the concept of exceptions in Java
Potatoes. Understanding the concept of exceptions in Java is vital before we delve into the try-catch block. Any event that affects the normal functioning of a Java program is termed as an exception. For example, if you try to divide a number by zero, access a null pointer, or read a file that does not exist, then the exception is caused. In simple terms, exceptions usually occur when the program comes across an error or an abnormal condition.So when an event does happen and the Exception occurs, the particular Java program's execution environment (JRE) creates an exception object which includes info about the problem that the program has encountered. The Java program is said to crash if such an abnormal condition is not handled. In simple terms, the Abnormal exit of the program is when the program terminates at any point when an exception arises. Exception handling takes care of such scenarios.
The following are the two primary exceptions categories that can be encountered in Java programming language:
- Checked exceptions : These are exceptions that are checked at compile time. They represent the exceptions, which a method has to catch or which are incorporated in the method signature using the throws clause. Some of them are : `IOException`, `SQLException`, etc.
- Unchecked exceptions : These are the exceptions that do not need to be handled and occur during the execution of the program. They are the logical errors in the code of a program, for instance, `NullPointerException`, `ArrayIndexOutOfBoundsException`, etc.
2. The Try-Catch Block
The try-catch block combines both of the aspects of exception handling in Java. It comprises two principal components:1. Try block : This is where the code that is likely to throw an exception is written. It is contained in a `try` statement.
2. Catch block : The system executes this block in case the code in the `try` block results in an error. It comes after the `try` statement and is preceded by the `catch` statement.
The raising of exceptions in the `try` block results in stopping the execution of all other remaining statements within that `try` block, and instead shifts the control to the corresponding `catch` block. The `catch` block then handles the exception, typically by logging the error, showing an error message to the user, or performing a corrective action.
2.1 How It Works
- The control enters the `try` block, and hence, each of the statements in the block is executed one after another.- In case an exception occurs at any time during the execution of the code in the `try` block, control shifts to the relevant `catch` block.
- The `catch` block rescues the exception thrown and executes the appropriate code written within it. Next, the program picks up from the code placed after the `catch` block.
The type of exception the catch block will deal with can be stated inside the block. This provision enables one to handle different types of exceptions in a more specific and efficient way.
3. Multiple Catch Blocks
A single `try` block can have more than one `catch` block in Java. This enables the program to deal with the same exception in different ways. For instance, a certain method can have several tasks to be performed whose tasks would all reside in the `try` block and cope with throwing an exception. If that's the case, one can provide multiple `catch` statements for managing every task.When several `catch` clauses are used, each `catch` clause can handle a single exception. So, it does not avoid `try` and `catch` when an exception is thrown, but rather, tries to find the appropriate `catch` statement for the exception. This feature helps to have a systematic way of handling the exceptions, as a particular `catch` is dealt with by letting other exceptions go.
3.1 Catching General and Specific Exceptions
Different exceptions can be specified in each `catch` block. When a more certain exception occurs, that specific `catch` block will fetch. When any such specific exception doesn't occur, the program can execute a rather wide `catch` block.But there is no `catch` without an exception. Exceptions are also handled in a nested manner. A more specific exception should be written before a more general exception. This is as a rule because when a `catch` block catches an exception no more blocks are run. If we have a specific block for one exception then definitely a general one must have been defined at a certain level of the hierarchy of exceptions, but that will be useless as the specific block would never be reached.
4. The Finally Block
Java also contains the other block as `finally` besides the `try` and `catch` blocks. This will contain the code that will always execute after the try-catch implementation irrespective of presence of an exception. The block is employed in dealing with system resources allocated during the process, for instance, closing files or releasing particular files opened in the `try` block.As we know, a `finally` block has no conditions; it will always execute whether or not an exception has been thrown. If an exception was not encountered at all, then the `finally` block is always executed after the `try` block. Therefore it is used to perform cleaning operations, such as freeing memory, closing database connections, and closing file streams.
The optional requirement of a 'finally' block should be emphasized. If no clean-up activity is required, it can be ignored. However, in the case of resources that need to be freed, a 'finally' block is essential.
5. Nested Try-Catch Blocks
There could be situations where an exception may arise not only in the outer `try` block but also in the `catch` block. In such cases, multiple try-catch blocks can be used. This reduces the scope of the error handling and makes sense in cases where an operation that could lead to more than one exception might be complicated.Although nested try-catch blocks are convenient they should be employed with caution. This can cause the code to be convoluted and difficult to read so it is best to avoid this feature unless the situation absolutely requires it.
Article
Be the first comment