In what situations is the throws keyword useful? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

In what situations is the throws keyword useful?

I understand the syntax and that I can pass over exceptions manually using throws and throw,however in what situations is this ACTUALLY of use? Thanks a lot :)

29th Jan 2018, 8:15 PM
Alexander E
Alexander E - avatar
1 Answer
+ 2
You might have some utility method which throws an exception, if the method signature includes a throws statement for a checked exception then it must be dealt with higher up the call chain. You probably want the higher level method to deal with it. For example, suppose you have a UI built in Java. When you click on a button it calls some method which has a catch block for various exceptions to send error messages to the UI. An exception is thrown up the call chain and caught in the top method to display the error nicely
29th Jan 2018, 8:44 PM
Dan Walker
Dan Walker - avatar