How to call a method that throws an exception in catch block? - java

I am trying to have an HandleException method that can handles various exceptions.
The problem is, my function returns a value. But if I use HandleException in my catch block, Java complains that the function does not return a value even though my HandleException always throw an exception.
What is a good way to fix this? Thanks!
Here is a sample code.
public class MyException {
static int foo(int num) throws Exception {
try {
return bar(num);
} catch (Exception e) {
handleException();
// throw new Exception("Exception in foo", e);
}
}
static int bar(int num) throws IllegalArgumentException {
if (num < 0) {
throw new IllegalArgumentException("Num less than 0");
}
return num;
}
static void handleException(Exception e) throws Exception {
System.err.println("Handling Exception: " + e);
throw new Exception(e);
}
public static void main(String[] args) throws Exception {
int value = foo(-1);
}
}
In my original class, I have lot of methods that have this format.
try {
...
} catch (Exception1) {
Log exception
throw appropriate exception
} catch (Exception2) {
Log exception
throw appropriate exception
}
I am trying to comeup with a cleaner way to write the catch blocks.

This is because the exception that was thrown on handleException is already caught by the catch block of the foo method. Thus the foo method no longer throws an Exception making the catch block return nothing. So if the bar method throws an exception it will go to the catch block but since the catch block is not returning anyting, Java executes the lines after the catch block but when it reaches the end it throws an error that "the method must return a result of type int" since you do not have a return statement.
You should change this part.
public class MyException {
static int foo(int num) throws Exception {
try {
return bar(num);
} catch (Exception e) {
throw handleException(e); // this will throw the exception from the handleException
// throw new Exception("Exception in foo", e);
}
}
static int bar(int num) throws IllegalArgumentException {
if (num < 0) {
throw new IllegalArgumentException("Num less than 0");
}
return num;
}
// This method now returns an exception, instead of throwing an exception
static Exception handleException(Exception e) {
System.err.println("Handling Exception: " + e);
return new Exception(e);
}
public static void main(String[] args) throws Exception {
int value = foo(-1);
}
}

In my original class, I have lot of methods that have this format... I
am trying to come up with a cleaner way to write the catch blocks.
I think the issue is more to do with understanding how the exceptions are handled in applications; its a design issue, in general.
Consider the method: int foo(int num) throws Exception
The method foo returns a value, catches an exception/handles and also throws an exception. Consider these aspects.
If the method runs normally, without errors, it returns a value. Otherwise, if there is a problem with its logic, throws an exception within the method, catches it and handles it within the catch-block of the method. The method also throws an exception.
There are two options here to consider:
Re-throw an exception, like a custom business/application exception (just log it and re-throw the same or a custom exception), which needs to be handled elsewhere - that is in a calling method up the stack.
Handle the exception: This means that the method takes care of the exception. Some business/application logic happens within the exception handling. And, the method returns a value.
The purpose of a method throwing an exception is that it is handled elsewhere, like in a calling method. The handling can be like recovering from the exception problem or displaying a message or aborting a transaction or whatever the business logic defines.
Is the exception thrown because of a business logic issue? If so it is likely that you show a message to the user or do some other logic about it and/take further steps to recover from it - as the business rules permit it.
In case the exception is thrown as a result of a situation which is not recoverable by the application's logic, do appropriate actions.
Ultimately, you have to have a clear requirement about why an exception is thrown, what you do with the exceptions thrown and how you handle them in the application. The application/logic/rules requirement influences in designing the exception handling in the code.
Notes (edit-add):
There are quite a few articles which explain about exception handling
in business application out there on the net. One can try a search
string like "Java exceptions best practices". Here is one such
article which has some useful info: Effective Java
Exceptions.
There is also the try-catch-finally construct to consider (along with various new exception features introduced with Java 7).

foo method's return type is int and return type of handleException is void, that is why compiler gives error.
(1) Here you could solve this as follows:
Throw exception as is again.
try{
return bar(num);
}
catch(Exception e){
handleException(e);
throw e;
}
(2) Moreover if you want to throw new created exception then change return type of handleException to Exception. Use
throw handleException(e);

Related

How to handle an exception in Java thrown by a method into another method?

Let's suppose I have this class:
public class Obj1{
...
public void do_Something(int someParameter) throws SomeException {
if(...) throw new SomeException();
...
}
...
}
then, somewhere
public class Obj2{
...
public void do_SomeOtherThing(Obj1 obj1){
obj1.do_Something();
//apparently the only solution is try-catching it directly, even if I'm not in the main...
...
}
I've learned that exceptions should only be thrown by METHOD, and catched by MAIN, so, my question is: is try-catch the unique way to handle sub-method exceptions, or the most external method (do_SomeOtherThing) will throw it, so that I can try-catch it directly in main, deleting the try-catch in Object2 class?
Basically, can I do as follows?
public static void main(String[] args){
Object1 obj1 = new Object1();
Object2 obj2 = new Object2();
try{
obj2.do_SomeOtherThing(obj1);
}
catch(SomeException e){
...
}
}
or not?
A checked exception is part of the contract that a method has with its caller, and a thrown exception will always need to be handled one way or another.
The correct answer depends on the exact situation:
The caller can handle the exception:
String getStringFromRemoteServer() throws IOException { ... }
String getConfigString() {
try {
return getStringFromRemoteServer();
} catch (IOException e) {
LOG.warn("Failed to contact server, using local version.", e);
return getLocalString();
}
}
In this case we have an alternative source of the data we need, so if the preferred method fails we catch the exception, log it (so that we know a problem exists with our network) and call the alternative.
The exception is fatal, and we don't want any function higher in the call tree to try to handle it.
Configuration parseConfiguration(String configString) throws ParseException { ... }
void loadConfiguration() {
try {
this.globalConfig = parseConfiguration(getConfigString());
} catch (ParseException e) {
throw new RuntimeException("Corrupted config", e);
}
}
In this case an exception means that the configuration of our application is fatally broken. There is no point in trying to handle this error, and no point in any of our callers trying to handle it, so declaring throws on loadConfiguration() would just be confusing clutter. We wrap the exception in a RuntimeException and rethrow it. Note that we don't log it -- there will be some top level reporting of uncaught exceptions, so logging it here would be repetition.
It is still valuable to have parseConfiguration() throw a checked exception, because when we are calling it from the interactive configuration editor we catch the exception and display an error message to the user.
Maybe our caller can handle the exception.
int stringToInteger(String s) throws BadNumberException { ... }
String decimalStringToHexString(String s) throws BadNumberException {
return intToHex(stringToInteger(s));
}
In this case we are not changing the meaning of the exception -- decimalStringToHexString is converting a number from a string, and one possible outcome is that the string is illegal. Our caller needs to be aware of that as a possible outcome, just as callers of stringToInteger() are, so we simply declare the exception and let our caller handle it. Our caller knows the context they are using the number in, so they can decide how to handle the exception.
A couple of rules:
Never completely ignore an exception (OK, maybe InterruptedException). If you write try { ... } catch (Exception e) {} the empty catch clause will make it hard to spot why your code doesn't work.
When you wrap an exception, always include the original exception as the cause.

Intercept and rethrow a Java unhandled exception

I've coded a method with a catch-all handler, but I need to rethrow the exception as if it were unhandled, so that a caller (much) further up the call stack can handle it. The trivial way to do this is simply:
try {
...
} catch (Exception ex) {
// do something here...
// and rethrow
throw ex;
}
But the problem is that, because of the throw statement, Java requires this method to declare itself as throws Exception, which in turn, requires all the callers to handle the exception or declare themselves as throws Exception. And so on up the call chain...
Is there any simple way to rethrow the exception as if the current method did not handle it?
You have exactly two options with (checked) exceptions:
Handle them in the method via a try/catch (which may include rethrowing as a different exception type)
Declare that the method throws the exception.
If you want to rethrow the exception as if this method did not catch it, your only option is 2.
Note: you only want to catch (Exception e) if a method in the try block actually throws Exception. Otherwise, catch the specific exception types.
You could do what #radoh has said and just wrap into a RuntimeException, but one downside of this is your stacktrace is now polluted and will show the offending line to be where you declare throw new RuntimeException(ex).
An alternative is to use Lomboks SneakyThrows mechanism, like this:
public static void main(String[] args) {
methodWithException();
}
private static void methodWithException() {
try {
throw new Exception("Hello");
} catch (Exception e) {
Lombok.sneakyThrow(e);
}
}
Your stacktrace will remain intact, but you no longer need to declare throws Exception.
It's worth reading the documentation on why you should/shouldn't do this

What is the difference between propagating and handling?

I'm studying for a CS exam this Friday and have hit a bump in the road here. The question asks me to handle the exception and then propagate the exception using two different methods, but I was under the impression they were the same thing. Can anyone help? The practice question is listed below.
You are given the following class:
public class ReadData {
public void getInput() {
getString();
getInt();
}
public void getString() throws StringInputException {
throw new StringInputException();
}
public void getInt() throws IntInputException {
throw new IntInputException();
}
}
class StringInputException extends Exception {}
class IntInputException extends Exception {}
The code above will result in compilation errors in getInput() method.
Rewrite getInput() method using two different techniques:
Method 1 - Handle the exception
Method 2 - Propagate the exception
so that the code compiles.
They are not the same thing. Propagation basically means re-throwing the exception, that is, allowing somewhere further up in the code to handle it; generally this is done if nothing can be done about the exception at the current level. Handling the exception means catching it and actually doing something about it - informing the user, retrying, logging - but not allowing the exception to go any further.
class Example {
// a method that throws an exception
private void doSomething() throws Exception {
throw new Exception();
}
public void testHandling() {
try {
doSomething();
} catch (Exception e) {
// you caught the exception and you're handling it:
System.out.println("A problem occurred."); // <- handling
// if you wouldn't want to handle it, you would throw it again
}
}
public void testPropagation1() throws Exception /* <- propagation */ {
doSomething();
// you're not catching the exception, you're ignoring it and giving it
// further down the chain to someone else who can handle it
}
public void testPropagation2() throws Exception /* <- propagation */ {
try {
doSomething();
} catch (Exception e) {
throw e; // <- propagation
// you are catching the exception, but you're not handling it,
// you're giving it further down the chain to someone else who can
// handle it
}
}
}
"Handle the exception" means to catch it and do whatever is necessary to continue normally.
"Propagate the exception" means to not catch it and let your caller deal with it.
dictionary.com is your friend in this one.
Sometimes we have to deal with that pesky english language.
Handle means do something with the exception,
abort program ,
print error,
mess around with data ...
propagate it means forward it onto someplace else i.e. re throw it.

Why try/catch around throwable?

In trying to refactor some I code I attempted to throw the exception in the catch clause like so -
try {
....
}
catch(Exception exception){
.....
throw exception
}
However when I attempted to throw the exception on line "throw exception" the compiler complained with a message that I needed to surround my throw clause in a new try/catch like so -
try
{
....
}
catch (Exception exception)
{
.....
try
{
throw exception
}
catch (Exception e2)
{
...
}
}
Why does the compiler require this and what use does it provide ?
Thanks
The exception java.lang.Exception is a checked exception. This means that it must either be declared in the throws clause of the enclosing method or caught and handled withing the method body.
However, what you are doing in your "fixed" version is to catch the exception, rethrow it and then immediately catch it again. That doesn't make much sense.
Without seeing the real code, it is not clear what the real solution should be, but I expect that the problem is in the original try { ... } catch handler:
If possible, you should catch a more specific exception at that point, so that when you rethrow it, it is covered by the method's existing throws list.
Alternatively, you could wrap the exception in an unchecked exception and throw that instead.
As a last resort, you could change the signature of the method to include Exception in the throws list. But that's a really bad idea, because it just pushes the problem off to the caller ... and leaves the developer / reader in the position of not knowing what exceptions to expect.
In Java, there is a distinction between checked and unchecked exceptions. An unchecked exception can essentially be thrown at any place in code and, if it's not caught somewhere, it will propagate up to the entry point of your application and then stop the process (usually with an error message and stack trace). A checked exception is different: The compiler won't let you just let it propagate, you need to either surround any code which might throw a checked exception with try-catch blocks (and "throw exception" is the simplest case if exception is an instance of a checked exception class) or you must mark the method which contains the call to code that might throw a checked exception with a "throws" declaration. If the desired behaviour is to throw an unchecked exception, then you'll need to wrap the exception in a RuntimeException. If the desired behaviour is to keep the exception checked, then you'll need to add a throws declaration to your current method.
In your original code, nothing catches the thrown exception. I would imagine you either have to specify that your function throws an exception or have another try/catch block as the compiler suggests to catch it.
Instead of
public void yourFunction(){
try {
....
}
catch(Exception exception){
.....
throw exception
}
}
try
public void yourFunction() throws Exception{
try {
....
}
catch(Exception exception){
.....
throw exception
}
}
My guess is that your trying to throw an exception sub class that isn't declared by the method as an exception type it can throw.
The following example works
package test.example;
public class ExceptionTest {
public static void main(String[] args) throws Exception{
try {
int value = 1/0;
} catch (Exception e) {
System.out.println("woops the world is going to end");
throw e;
}
}
}
However this example will give an error.
package test.example;
public class ExceptionTest {
public static void main(String[] args) throws RuntimeException{
try {
int value = 1/0;
} catch (Exception e) {
System.out.println("woops the world is going to end");
throw e;
}
}
}
Note in the second example I'm simply catching Exception not RuntimeException, it won't compile as I throw Exception which is an undeclared throws, even though I do declare RuntimeException.
Yes the exception is a RuntimeException but the compiler doesn't know that.
Just thought of a third working example to show you. This one also works because your throwing the same type as you declare. (note the only change is the catch block)
package test.example;
public class ExceptionTest {
public static void main(String[] args) throws RuntimeException{
try {
int value = 1/0;
} catch (RuntimeException e) {
System.out.println("woops the world is going to end");
throw e;
}
}
}
You need to understand the differences between all three of these answers

Magic Exception thrower without declaring throws Exception

I want a method that can throw any Throwable including sub classes of Exception. Ive got something that takes an exception, stashes it in a thread local, then invokes a class.newInstance. That class ctor declares that it throws Exception then takes the threadlocal and throws it. Problem is it does not work for the two declared Exceptions thrown by Class.newInstance() namely IllegalAccessException and InstantiationException.
Im guessing any other method using some sun.* class is just a hack and not really reliable.
Wrapping is not an option because that means catchers are catching a diff type and that's just too simple and boring...
static public void impossibleThrow(final Throwable throwable) {
Null.not(throwable, "throwable");
if (throwable instanceof RuntimeException) {
throw (RuntimeException) throwable;
}
if (throwable instanceof Error) {
throw (Error) throwable;
}
try {
THROW.set((Exception) throwable);
THROWER.newInstance();
} catch (final InstantiationException screwed) {
throw new Error(screwed);
} catch (final IllegalAccessException screwed) {
throw new Error(screwed);
} finally {
THROW.remove();
}
}
private final static Class<Impossible> THROWER = Impossible.class;
private final static ThreadLocal<Exception> THROW = new ThreadLocal<Exception>();
static private class Impossible {
#SuppressWarnings("unused")
public Impossible() throws Exception {
throw THROW.get();
}
}
From Java Puzzlers (puzzle 43):
public static void impossibleThrow(Throwable t)
{
Thread.currentThread().stop(t); // Deprecated method.
}
The book shows other methods of achieving the same problem, one is a simplified version of yours, the other exploits generic type erasure to throw any Throwable where an Error is expected.
If you want an Exception to bubble up through code not expecting that exception then just wrap it in a RuntimeException
} catch (RuntimeException e) {
throw e; // only wrap if needed
} catch (Exception e) {
throw new RuntimeException("FOO went wrong", e);
}
Remember to let the message be informative. Some day you will have to fix a bug based only on the information in the stack trace.
Wrapping an exception inside a RuntimeException (as suggested by Thorbjørn) is the way to go. However, you usually want to maintain the stacktrace of the original excpetion. Here's how:
public static void rethrow(final Throwable t)
{
if(t instanceof RuntimeException)
throw (RuntimeException) t;
RuntimeException e = new RuntimeException(t);
e.setStackTrace(t.getStackTrace());
throw e;
}
I patched javac to remove the error, compiled impossibleThrow(), renamed the source file to something that does not end in .java (which forces the next compile to use the existing .class) and used that.
There is some validity for this question as a debugging tool. Suppose you are working with some code that may have failed and you see that it (perhaps) catches certain exceptions and (perhaps) throws certain exceptions. You suspect that an unexpected exception was not caught. However, the underlying code/system is too complex and the bug is too intermittent to allow you to step through in the debugger. It can be usefull to add the throwing of an exception without changing the method in any other way. In this case, wrapping the exception with a RuntimeException would not work, because you want the calling methods to behave normally.

Categories

Resources