3

These days, I have been trying to learn java, But I am confused about Throws exceptions. is this statement correct?: Throws in method signature causes an exception (or some exceptions) throw to another method that is calling this method. and caller method has try/catch to handle thrown exception.

Thanks in advance. Farrohk

2 Answers2

3

Neither statement is correct.

Throws in method signature causes an exception (or some exceptions) throw to another method that is calling this method

Declaring that a method throws an exception just indicates that it might throw the exception, not that it does.

and caller method has try/catch to handle thrown exception.

The caller can declare that it throws the same exception, rather than try/catching, so the exception is propagated to that method's caller.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Thank you, suppose we have _method_ which declare by throws an exception that it might throw the exception. Now if the _method_ throw an exception, what will happen? Where would be handle that exception? If nothing will happen then there is no reason to declare this _method_ by throws. – Farrokh Yousefi Mar 31 '15 at 11:41
  • It will have to be handled by its caller, either `catch`ing it or propagation via `throws`, which passes the burden of handling the exception onto the caller's caller. My second point is just saying that try/catch is not the only option. – Andy Turner Mar 31 '15 at 11:43
0

Throws clause in used to declare an exception and thow keyword is used to throw an exception explicitly.

This link which has good information about the difference between throws and throw can help you improve your understanding.

thots
  • 110
  • 5