rni902
rni902

Reputation: 377

Java: Using Catch and throws in the one block?

What is the point of catching and then also throwing an Exception like below? Is it bad practice to do both?

  try{

    //something

    } catch (Exception e){

    throw new RuntimeException("reason for exception");
    }

Upvotes: 0

Views: 87

Answers (3)

secolive
secolive

Reputation: 589

Usually, such code is used to re-wrap exceptions, that means transforming the type of the exception. Typically, you do this when you are limited in what exceptions are allowed out of your method, but internally other types of exceptions can happen. For example:

class MyServiceImplementaiton implements MyService {
    void myService() throws MyServiceException { // cannot change the throws clause here
        try {
            .... // Do something
        } catch(IOException e) {
            // re-wrap the received IOException as MyServiceException
            throw new MyServiceException(e); 
        }
    }
}

This idiom enables to keep propagating exceptions to the caller, while conforming to the throws clause in the interface and hide the details of the internals (the fact that IOExceptions can happen).

In practice, this is always better than just calling e.printStackTrace() which will actually "swallow" the error condition and let the rest of the program run as if nothing had happened. In this respect, behaviour of Eclipse is quite bad as it tends to auto-write such bad-practice constructs if the developer is not careful.

Upvotes: 1

Eran
Eran

Reputation: 393771

In your example, an Exception is caught and a RuntimeException is thrown, which effectively replaces a (potentially) checked exception with an unchecked exception that doesn't have to be handled by the caller, nor declared by the throwing method in a throws clause.

Some examples :

This code passes compilation :

public void SomeMethod ()
{
    try {

        //something

    } catch (Exception e){
        throw new RuntimeException("reason for exception");
    }
}

This code doesn't pass compilation (assuming "something" may throw a checked exception) :

public void SomeMethod ()
{
    //something
}

An alternative to catching the Exception and throwing an unchecked exception (i.e. RuntimeException) is to add a throws clause :

public void SomeMethod () throws Exception
{
    //something
}

This is one use case of catching one type of exception and throwing another. Another use case is to catch one type of exception and throw another type of checked exception (that your method declares in its throws clause). It is sometimes done in order to group multiple exceptions that may be thrown inside a method, and only throw one type of exception to the caller of the method (which makes it easier for them to write the exception handling code, and makes sense if all those exceptions should be handled in the same manner).

Upvotes: 0

Thilo
Thilo

Reputation: 262474

This is called rethrowing an exception, and it is a common pattern.

It allows you to change the class of the exception (such as in this case), or to add more information (also the case here, as long as that error string is meaningful).

It is often a good idea to attach the original exception:

throw new RuntimeException("cause of the problem", e);

Rethrowing as an unchecked exception (a RuntimeException) is sometimes necessary when you still want to throw an exception, but the API of your method does not allow a checked exception.

Upvotes: 0

Related Questions