Try Block Raising Multiple Exceptions

Java Exception Multiple Catch Blocks

We know that a try block in Java will have a matching catch block. Now, we see how to use multiple catch blocks. Have a look at the picture.

The picture shows there are three code snippets within the try block. Let us assume code snippet 2 depends on code snippet 1 and code snippet 3 depends on code snippet 2. We will also assume each code snippet has the possibility of one exception type. From the picture, we can assume the exception types are A, B and C.

Separating Error Handling Code

Java Exception Handling: Try Catch Finally

When application crashes the JVM excludes it from any further execution. This means application halts abruptly. In Java Exception Handling is useful to avoid application crashes. Exception handles error condition and takes a safer path in the execution flow. The exception also makes code more readable as the error handling code stays in a separate section from the normal business logic code. In this example, we will see how to handle exception using Try & Catch blocks. We will also see the role of Finally block.

Java Boxing and Unboxing

Java Wrapper Class & Boxing, Unboxing

A Wrapper Class in Java wraps primitive type and provides member methods around it. We call the process of wrapping as Boxing and reverse of it as Unboxing. With the use of a Wrapper Class we can see a primitive type as an object. Java collections do not support primitive types. But it can store objects. So, to store primitive types in a collection we can wrap it using a wrapper class. After wrapping a primitive using wrapper, we can use the member methods of the wrappers which act on the primitive type. The examples are:

Converting a double value to string.
Converting an integer to a long type.

We can also get back the primitive type from its wrapper. This is called Unboxing. In this article, we will see the examples to get better in sight on the Boxing and Unboxing.

Java Reflection - Types Relation

Java Reflection: Finding Instance Type

This example shows how to use various Java Reflection techniques to find the Instance types while the application is running in JVM. This example creates some sample class instances at run-time and then finds its type through Reflection techniques such as class.forName, getClass etc.