Exception Chaining in Java

Exception chaining in java allows us to relate one exception to another exception. An application often responds to an exception by throwing another exception. In effect, the first exception causes the second exception. It can be very helpful to know when one

Read More

Difference between Throw and Throws in Exception Handling

We have seen examples of Throw and Throws in some of our previous posts. Throw and Throws keyword are used in different scenarios while doing exception handling. In this post we see some basic difference between Throw and Throws. Difference between Throw

Read More

User Defined Exceptions in Java

While working on an application, you may come across some scenarios where you feel like the Exceptions defined in the java platform are not enough to represent the error/issue you want to tell the end user. In that case you may choose

Read More

Rethrowing of Exception in Java

Rethrowing of exception in java has improved starting from Java 7. We can now specify more specific exceptions using throws keyword. Before java 7: [crayon-58d6ff598adda075810930/] In the above method, the try block can throw either UserException1 or UserException2, this exceptions are caught

Read More

Catching Multiple Exceptions in Java

Catching multiple exceptions in java is now very easy starting from Java 7. Before java 7 we used to write the below code for catching multiple exceptions [crayon-58d6ff598b1c1199486230/] But since Java 7 we can easily club some of the exceptions,so that code

Read More

Throws in exception handling in java

Throws keyword in java is used to declare exceptions that can be thrown by the method. Throws keyword can be used to throw multiple exception. syntax: 1. Throws single exception [crayon-58d6ff598b657993783899/] example: [crayon-58d6ff598b660701504351/] 2. Throws multiple exception [crayon-58d6ff598b666366101225/] example: [crayon-58d6ff598b66a020521151/]   When

Read More

How to Throw exception in java

Exceptions in java can be thrown using the “throw” keyword. The throw statements requires a object to throw. This object needs to be a throwable object. Syntax: [crayon-58d6ff598bb7c428211946/] In java we can throw both checked and unchecked exceptions. Lets see some examples

Read More

Try Catch Finally in Java Exception handling

In this post we will see the exception handling using the try catch finally in java. 1. Use of Try – Catch Block A exception in java can be handled using a Try and Catch block. Lets see a simple example where

Read More

Return Statement in try catch and finally block

If you have a try catch finally block in java, the return statement behaves differently. You may think 1. If I have a return in try then will finally block get called? 2. What if a exception occurs? 3. What if catch

Read More

Try-with-resources Statement – Java 7

Many a times we forget to the close the file/jdbc resources that we open inside a try block after the operations are complete. This can cause memory leak issue. Starting from JDK 1.7, a new feature try-with-resource was introduced.This feature allows user

Read More