2

--the effects of case 1 and 2 are the same, why need to add the exception declaration in method signature?


//case 1

public void doSomething() throws Exception {
        //do Something
    }       
public void Caller() {
   try {
     doSomething();
   } catch (Exception e) {
     //handle the exception   
   }
} 

//case 2

public void doSomething() {
        //do Something
    }       
public void Caller() {
   try {
     doSomething();
   } catch (Exception e) {
     //handle the exception   
   }
} 

reference: what is the use of throws Exception

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
kevin
  • 55
  • 6

4 Answers4

2

The throws declaration is used to declare which checked exceptions your method throws.

For instance, if I write this code:

public void doSomething() throws SQLException {
}

any code that calls this method must have a try/catch block for SQLException, Exception, or Throwable... or it can have its own throws declaration for one of those 3.

Community
  • 1
  • 1
Powerlord
  • 87,612
  • 17
  • 125
  • 175
0

Unless it's an exception that you want to handle immediately in that method, it's good practice to use throws [specific Exception] so that the exception can be handled further up in the code.

It's somewhat commonplace to have a generic Throwable catch at the top that "gracefully crashes" in case something goes wrong.

  • It's not good practice to use `throws Exception` ever, for the simple reason that this obfuscates what exceptions can actually be thrown and forces *all* callers to have an overly generic `catch (Exception e)` or propagate the lack of clarity using its own `throws Exception`. Unless you mean that they should use `throws [some specific Exception]`? – jpmc26 Nov 21 '14 at 19:26
  • Yes I meant throws some specific exception so it can be handled higher up the call stack – Michael Goldstein Nov 21 '14 at 19:34
  • If you clarify that in your answer, I'll remove the downvote. (I can't remove it until you edit.) – jpmc26 Nov 21 '14 at 19:56
0

In this case, there is no difference, except that you're alerting the compiler to an exception that you're not going to be throwing.

It's also a bad idea to catch throw "Exception" - in both cases, you want to deal a particular exception that has a particular meaning. When you're catching, the only reason to use a try block is if you expect a particular exception, so you should catch that one. This way, if some unexpected exception comes up, you don't try to handle it the wrong way. (instead, your program fails, and you know there's a condition you have to deal with) When you're throwing, you want to throw a particular exception, either one you make up, or a standard one that has a known meaning, so the calling function knows what to deal with. For example, if your doSomething might throw an ArrayIndexNotFoundException if the widgets are not frobnicated, you might want to catch the ArrayIndexNotFoundException and throw a WidgetNotFrobnicatedException. Any time you throw an exception, your javadoc should specify exactly what circumstances will trigger that issue, so the user of your code has a chance to address this possible failure.

(there is one circumstance when I can see catching Exception, and that's if you want to fade to some graceful halt if things go wrong unexpectedly - in that case, in your catch block you'd log the issue, possibly alert a developer, and throw up some sort of "Sorry, error number 3542 has occurred, please restart the program" message.)

Jon Kiparsky
  • 7,499
  • 2
  • 23
  • 38
  • your answer is clear and also provide the useful information and suggestion other than my question, Thanks very much!! – kevin Nov 22 '14 at 17:40
0

If your doSomething method, has the chance to throw an exception and you don't want to catch it, you should add this throws Exception on the method.

Dr. Programmer
  • 368
  • 1
  • 7
  • 19