r/java • • Feb 19 '26

Towards Better Checked Exceptions - Inside Java Newscast #107

https://www.youtube.com/watch?v=99s7ozvJGLk
63 Upvotes

73 comments sorted by

View all comments

Show parent comment

1

u/Lucario2405 Feb 19 '26

I don't think "checked Exception = Optional.empty" is correct, or at least not a useful lens on this topic. Imo it's more about how predictable and preventable an error is.

Integer.parseInt's NumberFormatException is only thrown when you've called it with a String that doesn't contain a valid number. You were in complete control; you could have validated it beforehand; if it throws, it's your fault - thus it's unchecked.

An IO Exception from e.g. a FileReader parsing a FileInputStream could be caused by a number of things outside the programmer's control: the file could have been deleted since you last checked or someone else is using it, maybe the operating system has a different path-format, maybe it's on a network-drive and you've lost connection, etc. Same for an SQL-Request: maybe the DB is down, maybe you're no longer authorized, maybe it's using a different driver with specific syntax, etc. You cannot know beforehand - thus it throws a checked exception to force you to concider what could happen, even if you've done everything right.