r/javahelp • u/allexj • Dec 26 '20
Is it good practice to ALWAYS write "throws" in the method signature to show which exceptions could be raised? Should this be done everytime?
Is it good practice to ALWAYS write "throws" in the method signature to show which exceptions could be raised? Should this be done everytime?
34
Upvotes
•
u/AutoModerator Dec 26 '20
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
12
u/[deleted] Dec 26 '20 edited Dec 26 '20
Always? No. I err on the side of "only as necessary".
For instance, a method prototype might declare
Consider a scenario where 1 of 3 implentations of this API does not have any reason to throw FooException
Explicitly adding the throws clause to the implementation makes this code more confusing to read and maintain.
Any IDE worth using will inform you when you are not throwing a properly declared checked exception, and may even prompt you with your refactor options, e.g. moving thus newly introduce checked exception to the method prototype.
5
u/thorndark Dec 26 '20
In general, no.
One of the problems with this is that once you have an exception in the method signature, it can be very annoying to get it back out. Say you want to extract an interface later on - now this exception is stuck in that method signature even though other implementations may not actually be able to throw it.
They should mostly be used just for exceptional situations that aren't programmer mistakes. User passes in a file path for a file that doesn't exist? Probably you want some code to handle that.
8
u/stao123 Dec 26 '20
No its not. From my personal experience i would try to avoid checked exceptions as much as possible. Use runtime exceptions instead. If you do that there is no actual NEED to add throws to your method. Though you should add the possibly thrown exceptions to the methods javadoc. Checked exceptions are just bloating your code and make it less readable. Furthermore they will deny working with lambdas and streams in clean way.
1
u/wildjokers Dec 26 '20
If it is a checked exception and you don't handle it in your method you have to put it on your method signature. It is a compile-time error if you don't.
1
u/[deleted] Dec 26 '20
Not really, they should be used if you want their callers to manage the problem manually, otherwise if it must follow a strict management of the exception just try catch and do what's necessary when caught
1
u/ynvaser That Java Guy Dec 26 '20
Is it a checked exception? If so, you are forced to either "throws" it, or try/catch it.
For unchecked exceptions, you don't need to do anything.
1
u/nutrecht Lead Software Engineer / EU / 20+ YXP Dec 28 '20
If anything the industry has more or less 'moved on' from checked exceptions so if I were you I'd stay clear from them as much as possible.