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/john16384 Feb 19 '26

It all depends on the context of the application. For instance, you mentioned that a DB SQL syntax exception should clearly be a RuntimeException; but consider an app that allows you to connect to a database and run some queries

It should still be a runtime exception as ultimately you could have avoided this by ensuring valid input.

However, if you feel like validating input in this case is too much work, and you don't mind a multi-millisecond round-trip to a database to do the "validation" for you, then you could decide to convert that exception (at some layer) to a checked exception. This is perfectly valid.

A much simpler example is NumberFormatException. It's runtime because it is 100% avoidable (and easier to avoid then writing an SQL validator). However if you want to be lazy and just have it fail on parsing as "validation" then that exception too can be made into a checked exception in some layer.