Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throws rule in java

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

like image 848
Farrokh Yousefi Avatar asked Mar 25 '26 14:03

Farrokh Yousefi


1 Answers

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.

like image 198
Andy Turner Avatar answered Mar 28 '26 03:03

Andy Turner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!