Skip to main content

Multiple catch Statements

Multiple Catch Statements
It is possible to have more than one catch statement in the catch block as illustrated below:
………………………
try
{
            statement;       // generates an exception
}
catch (Exception-Type-1 e)
{
            statement;       // process exception type 1
}
catch (Exception-Type-2 e)
{
            statement;       // process exception type 2
}
catch (Exception-Type-N e)
{
            statement;       // process exception type N
}
…………………………….
…………………………….

When an exception in a try block is generated, the Java treats the multiple catch statements like case in a switch statement. The first statement whose parameter matches with the exception object will be executed, and the remaining statements will be skipped.
Note that Java does not require any processing of the exception at all. We can simply have a catch statement with an empty block to avoid program abortion.
Example:
            catch (Exception e);
The catch statement simply ends with a semicolon, which does nothing. This statement will catch an exception and then ignore it.
Using finally statement
Java supports another statement known as finally statement that can be used to handle an exception that is not caught by any of the previous catch statements. finally block can be used to handle any exception generated within a try block. It may be added immediately after the try block or after the last catch block shown as follows:
try
{
………..
}
catch (………)
{
………………..
………………..
}
.
.
finally
{
………………..
……………….
}

If a problem occurs during execution of the try block, the Java Virtual Machine (JVM) immediately stops executing the try block and looks for a catch block that can process that type of exception. Any remaining statements in the try block are not executed. The search begins at the first catch block. If the type of the exception object matches the type of the catch block parameter, those statements are executed. Otherwise, the remaining catch clauses are examined in sequence for a type match.

One of the biggest advantages of the Java exception handling mechanism is that it allows you to write software that is better organized and easier to understand. You no longer need to distribute code to check and handle errors throughout your program. Instead, a try block can be written and, if problems occur during its execution, control proceeds in a well-defined manner to the appropriate catch blocks.

When a catch block completes execution, control passes to the statements in the finally block. The Java compiler ensures that the finally block is executed in all circumstances. When a try block completes without problems, the finally block executes. Even if a return statement is included in a try block, the compiler ensures that the finally block is executed before the current method returns.

The finally block is optional. However, in some applications it can provide a useful way to close files or databases at this point. Each try block must have at least one catch or finally block. Otherwise, a compiler error occurs. The compiler also checks the type of each catch block parameter.

Note:

Hereby, the article "Exception Handling" is finished. If there will be any more topics related to it, then I'll post that and also you are invited to send any request that you would like to see on this blog on my e-mail ID.

 Resources Used:

1. E Balagurusamy
2. SAMS Teach yourself

Compiled By: Chaudhary Amit V.



Comments

Popular posts from this blog

Characteristics of a Good Programming Language

Till now there are many high level languages which are very popular, and there are others, which could not become so popular in-spite of being very powerful. There might be many reasons for the success of a language, but one obvious reason is the characteristics of the language. Several characteristics believed to be important with respect to making a programming language good are briefly discussed below. Simplicity A good programming language must be simple and easy to learn and use. For example, BASIC is liked by many programmers only because of its simplicity. Thus, a good programming language should provide a programmer with a clear, simple and unified set of concepts which can be easily grasped. It is also easy to develop and implement a compiler or an interpreter for a programming language that is simple. However, the power needed for the language should not be sacrificed for simplicity. The overall simplicity of a programming language strongly affects the readability of the pr...

Angular 4 and Firebase Authentication: Email/Password

In our  previous  article we saw how to create authentication module using Google identity provider. Now we will see how to implement Email and Password authentication using Firebase. We will use Bootstrap form to create intuitive user interface for sign-up and login using Email and Password. Step 1: Create signUp component. ng generate component signUp Step 2: Create custom form in sign-up.component.html file. The result of above code: Step 3: Add two functions for creating the user and login using Email and Password in src/app/providers/AFAuth.ts file. Step 4: Call createUserWithEmailAndPassword function in service from sign-up.component.ts. Step 5: Update routing configuration in app.module.ts  to include signUp component. Step 6: Update Login form to have Bootstrap form. The result of the above code. Step 7: Enable Email/Password component in Firebas...

Angular 4 and Firebase Authentication: Setup

If you have come to this article, it means you are keen to learn new technology and that too Firebase. I love Firebase because it provides all basic but important features of any web application. Take for example, authentication, which is cumbersome and risky if not implemented with utmost care. And when we have the integration of Angular and Firebase, it becomes a lot easier for a developer to build such crucial modules in less time with minimal efforts. In this article we will create a simple Angular application using Firebase. I am going ahead with the understanding that you know the benefits of Firebase and have little knowledge about it. There are plenty of posts out on the web if you are behind. Step 1: Install Angular CLI (if not installed). npm install -g angular-cli Step 2: Create new Angular 4 project. By default now angular CLI will create Angular 4 project so you need not fret. ng new firebase-authentication Step 3: Check whether the ne...