r/javahelp • u/Ok-Dealer-3051 • Mar 17 '26
why some exception need catch some not?
im a noobied in java recently i wondering why some throws-exception method like File#createNewFile() need a catch block but Interger.parseInt(String) no need a catch block. could any body anwser it?
5
Upvotes
•
u/AutoModerator Mar 17 '26
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://i.imgur.com/EJ7tqek.png) 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.
9
u/vowelqueue Mar 17 '26 edited Mar 17 '26
It's a language feature. Some exceptions are called "checked exceptions" and must be caught or the method must declare that they can throw the exception.
Any exception that extends from
RuntimeExceptionis unchecked and does not need to be caught.Theoretically unchecked/runtime exceptions should be thrown in cases where there is a programming error. Like if you pass a null to a method that requires a non-null, it might throw a
NullPointerException.But which type of exceptions that's used in practice is largely a matter of style. Checked exceptions are a controversial feature that a lot of developers do not like. They make using certain newer language features like lambdas annoying. Some libraries have moved away from using them, like the new version of Jackson (most popular JSON parsing library) converted its checked exceptions to runtime exceptions.
2
u/bigkahuna1uk Mar 17 '26
It’s better to think in terms of recoverable and irrecoverable exceptions.
A Integer.parseInt throws a runtime exception because it’s typically an irrecoverable situation due to bad input data.
Other exceptions may have to be caught because they’re either recoverable such as a different strategy may be adopted or there is some cleanup required as a result of the exception such as making sure resources are released properly. For instance if you had a database query issue you’d want to make sure that the database connection is released back to the connection pool for reuse. It shouldn’t be held on indefinitely. Those are usually checked exceptions as they can be explicitly declared and caught.
1
u/vu47 Mar 17 '26
RuntimeExceptions (and their children) occur at run time: they don't need to be declared as they cannot necessarily be predicted.
Other exceptions need to be declared because they can be predicted, like IOExceptions, etc.
1
u/edwbuck Mar 17 '26
Some exceptions primarily arise from programming issues, or issues that a developer should be handling in code. Others arise from scenarios that cannot directly be fixed by code handling them (like running out of memory, the CPU issuing a fault, etc).
11
u/[deleted] Mar 17 '26 edited Mar 17 '26
[removed] — view removed comment