When a method throws and exception, do we need to have a try block inside the method?

For example,

    public void foo() throws SomeException{
        try{
            // content of method
        }
    }

Is the try block required? Or, is the method able to throw a SomeException without it :

    public void foo() throws SomeException{
            // content of method
    }

This is the case when we are not explicitly throwing a SomeException with throw.

有帮助吗?

解决方案

If SomeException is a checked exception you have to either

  • Use a try{}catch block or
  • Declare that your method throws it.

You do not have to do both, either example you show in your question works just fine.

The difference is that with the try clause you handle the SomeException yourself, whereas by declaring that your own method throws it you delegate the responsability of handling the SomeException to the calling method.

其他提示

When a method throws an exception it passes responsibility to handle exception to its caller. So you don't need to handle exception if you throw it in your signature. Like as follows.

public void foo(){
        try{
            // content of method
        }
    }

but if you write it this way.

public void foo() throws SomeException{

    }

you will call your method like as follows.

try{

   foo();
}

You don't need a try block.

public void foo() throws SomeException {
  // do some stuff
  // you decide to throw the exception by yourself:
  if (throwAnException) throw new SomeException();
  // or you call a method that throws SomeExpection:
  methodThatCanThrowSomeException();
  // do more stuff
}

As long as you declare it in your signature, you're prefectly fine. The caller of your method has to handle the exception, not you. So a caller might do:

try {
  foo();
} catch (SomeException e) {
  // handle exception
}

Or he might pass it further along by himself.

The most problematic case you'll regularly encounter is calling a method that declares a checked exception. In the great majority of real-life cases it is not appropriate to handle that exception at the spot, but let it propagate upwards. Unfortunately, Java makes you redeclare this same exception all the way up, which creates clutter, exposes implementation details, and often also breaks the contracts of existing methods.

In such a case the way to proceed is to wrap and rethrow:

catch (RuntimeException e) {throw e;} catch (Exception e) {throw new RuntimeException(e);}

1. If the method that we are calling from a program throws an Exception, then we need to usetry/catch around the method invocation.

2. Suppose we are writing a method that throws an exception, then we need to throw new Exception object from withing the method.

3. An exception is an object of type Exception. We have Checked Exception, and Unchecked Exception (Runtime Exception).

you don't essentially need to have a try block in it

      public void foo() throws SomeException {
// do some stuff
// you decide to throw the exception by yourself:
if (throwAnException) throw new SomeException();
// or you call a method that throws SomeExpection:
methodThatCanThrowSomeException();
// do more stuff
 }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top