Search code examples
javaexceptionmethod-signature

Java Method Signature Throws Exception, Implementation does not


I am looking into some exception one method is throwing. The method looks like this:

public void someMethod() throws someCheckedException{

  //doSomething statements
  //but no statements actually throws 'someCheckedException'

}

My question is that is it possible to make this method throw 'someCheckedException' while the implementation does not have a throw statement at all.

Is it wrong to put throw exception in signature without implement a statement throwing exceptions?


Solution

  • This is completely fine. The throw statement in method signature is there in case method throws actual exception declared in the throws statement and to usually pass handling to different class You just need to remember to put such method into try catch statement when you want to use it.