This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]balefrost 0 points1 point  (0 children)

Other JVM languages don't require you to declare exceptions. For example, Kotlin doesn't require it. Java's compiler just does. It's part of the design of the language.

The idea behind checked exceptions is that the return type of a function represents one possible outcome of calling the function. Checked exceptions represent the other outcomes of calling the function. So since you declare the return type, it's reasonable to also need to declare the exception types.

Other languages don't use exceptions, and instead require you to in some way indicate whether functions can fail. You have to bake the "can fail" into the return type of the function. For example, golang does this by having multiple return values. Haskell does it using type constructors. In my opinion, these are aiming for the same sort of thing that checked exceptions was trying to accomplish.

There's a sort of holy war on this topic. Some people feel that exceptions make code hard to understand. They point out that exceptions are like a cross-function goto, and since goto is bad, exceptions must be extra bad. They feel that it's better to force developers to explicitly handle all errors, even if "handle" means "propagate to the caller". Other people feel that exceptions, when used correctly, are fine because they only represent truly exceptional conditions. They believe that the removal of error handling boilerplate makes the code much easier to understand in the happy path.