Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.


+19 votes

Why do we need to mark methods with "throws SomeException" instead of just handling any exception within the method where the exception/error happens?

asked in CSC305 Fall 2022 by (1 point)

3 Answers

+6 votes
Best answer

Sometimes it is inelegant, inconvenient, or even impossible to correctly handle an exception within the method (let's call it methodA) where the exception arises. In those cases, it is better to add a "throws" declaration to *methodA, and then whatever method calls methodA(...) will need to consider whether it can handle the error (with a try... catch ... block), or whether it too is not well-situated to handle the error.

For example, somewhere in your data model, you might have a method called loadFromFile() that loads a GameBoard (or TerrainMap) object from a file. However, a FileNotFoundException (or other *IOException might occur when you attempt to read from the file.

Can we handle this exception properly within loadFromFile()? No!
This method could be getting called from within an action handling method for a JavaFX MenuItem, in which case, the appropriate response might be to display an Alert popup window to the user, and then perhaps ask them to select a file again. On the other hand, loadFromFile() could also be happening from a different console/command-line/terminal application whose job it is to open all .gameboard files in a folder and convert them each to .obj format. In this case, the error should be displayed to the console, and the user should be asked how to respond using a Scanner that's reading from System.in.
loadFromFile(...) has no way to know the broader context of how it is being used, so it should just add the throws IOException declaration to it's method header, and announce to the world that anyone who calls this method should be ready to handle the possibility of an IOException.

answered by (508 points)
+8 votes

I think by throwing exception at the method it checks the exception that can occur during execution, rather than checking at certain point inside the method while it is running.

For example, when you have a method called exportFile(File file) with a parameter type of File, it is better to mark IOException, etc with "throws" at the method rather than writing:
try {


} catch (IOException e) {

sysout.println("file not found.");

inside the method where other exceptions might occur during runtime.
Also, you want to throw an exception if it is caused by an input parameter like the example.

It will allow you to write a higher quality code where errors are checked during compile time rather than runtime.

If there is anything wrong, please correct me.

answered by (1 point)
+8 votes

To my knowledge, we mark methods with "throws SomeException" in order to keep track of a specific type of exception that is thrown for unavoidable reasons such as a file specified by user is not readable, or no network connection available, or other checked exception. In these cases, it's not programmer's fault to handle that particular exception, so it's better to alert the user what has happened and how to fix it easily (like input another readable file). By this way, we could stop the program unexpectedly and reports an exception for not anticipating before executing the program.

Handling any exception within the method is only when we all know where the exception/error happens and could anticipate with throw a try catch clause.

Hope this helps!

answered by (1 point)
edited by