In Java 7 Rethrow Exception feature added.I know it's concept but I want to see the real application of it and why this feature needed?
How to rethrow an exception in Java? Sometimes we may need to rethrow an exception in Java. If a catch block cannot handle the particular exception it has caught, we can rethrow the exception. The rethrow expression causes the originally thrown object to be rethrown.
Re-throw an exception we caught Re-throw wrapped exception The purpose of the rethrow operation is to propagate the exception to the higher level, But before propagation we would perform some activities like logging, send an email, audit error, rollback transaction … etc.
rethrow (exception) rethrows a previously caught exception, exception. MATLAB ® typically responds to errors by terminating the currently running program. However, you can use a try/catch block to catch the exception. This interrupts the program termination so you can execute your own error handling procedures.
Concurrently calling rethrow_exception on exception_ptr objects that refer to the same exception is safe. Note though that some implementations may not perform a copy of the pointed object on entering the catch exception handling block, and concurrently accessing the rethrown exception object in this case may introduce a data race.
I will take examples from here This is the example:
static class FirstException extends Exception { }
static class SecondException extends Exception { }
public void rethrowException(String exceptionName) throws FirstException, SecondException {
try {
if (exceptionName.equals("First")) {
throw new FirstException();
} else {
throw new SecondException();
}
} catch (FirstException e) {
throw e;
}catch (SecondException e) {
throw e;
}
}
This compiles with both java 6 an 7.
If you want to keep checked exceptions from method signature, you have to keep cumbersome catch clauses in java 6.
In Java 7 you can do it in following way:
public void rethrowException(String exceptionName) throws FirstException, SecondException {
try {
if (exceptionName.equals("First")) {
throw new FirstException();
} else {
throw new SecondException();
}
} catch (Exception e) {
throw e;
}
So your benefit is that you have less cumbersome catch clause.
Use Rethrowing Exceptions with More Inclusive Type Checking feature
in Java SE 7, you can specify the exception types FirstException and SecondException in the throws clause in the rethrowException method declaration
When you want to declare specific exceptions that can be thrown (mainly when you are catching a generic error)
For example see Precise Rethrow example:
public static void precise() throws ParseException, IOException{ try { new SimpleDateFormat("yyyyMMdd").parse("foo"); new FileReader("file.txt").read(); } catch (Exception e) { System.out.println("Caught exception: " + e.getMessage()); throw e; } }
This also make your code compliant to Sonar's Raw Exception rule.
Note you can similarly catch Throwable
Couple of use-cases:
Using rethrow, you can edit the stacktrace information to make it more accurate. Also when needed you can hide or rid off the unnecessary internal details in stack trace.
try { //... }
catch (Exception e) { throw (Exception) e.fillInStackTrace(); }
Real application of fillInStackTrace
is very well explained here:
Why is Throwable.fillInStackTrace() method public? Why would someone use it?
Quote from the book "Thinking in Java" written by Bruce Eckel:
If you want to install new stack trace information, you can do so by calling
fillInStackTrace( )
, which returns a Throwable object that it creates by stuffing the current stack information into the old exception object
Add custom message to the thrown exception. Add custom message to thrown exception while maintaining stack trace in Java
A simple example I can think of:
void fileOperator(String operationType) throws Exception(){ ... }
void fileReader() throws Exception {
try{
fileOperator('r');
}
catch(Exception e){
throw Exception("Failed to read the file", e);
}
}
void fileWriter() throws Exception{
try{
fileOperator('w');
}
catch(Exception e){
throw Exception("Failed to write the file", e);
}
}
Also, we can throw a more specific exception type (say FileReadException, FileWriteException) from the catch
block.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With