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
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 diff
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
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:
1 2 3 4 5 6 7 8 9 10 11 12 |
public void someMethod(String testStr) throws Exception { try { if (testStr.equals("SomeThing")) { throw new UserException1(); } else { throw new UserException2(); } } catch (Exception e) { //Do some processing.. throw e; } } |
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
1 2 3 4 5 6 7 8 9 |
try{ //Some Statements }catch(Exception1 e1 |){ //log statements }catch(Exception2 e2){ //log statements }catch(Exception3 e3){ //log statements } |
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-674d72e8b20
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-674d72e8b2317342
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 sim
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
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