I'm just curious, is there any other languages besides Java that uses checked exceptions ?
I did try to find information about this but couldn't find any answers.
I'm just curious, is there any other languages besides Java that uses checked exceptions ?
I did try to find information about this but couldn't find any answers.
The reason you couldn't find information on any other languages using checked exceptions is they learned from Java's mistake.
EDIT: So to clarify a little more checked exceptions were entirely a Java thing that in theory sounded like a really great idea, but in practice actually create a tight coupling between the consuming function and the function being consumed. It also makes it more tedious to handle an exception where it can be handled. Instead you have to catch and re-throw in every function in between where the exception was thrown and where it can actually be handled. I could rewrite it all here but I think this post does a magnificent job of explaining why checked exceptions are really not a good idea.
Checked exceptions are not a common feature in mainstream languages due to bad experiences with them in Java. However, Java is not the only language that implements them, and just because Java's implementation was faulty does not mean that they are a bad feature in general.
Nim has checked exceptions.
Checked exceptions have been implemented as a Purescript library.
Checked exceptions can be implemented in Koka by making a custom defined effect for it.
Some common issues with checked exceptions in Java can be handled with better design.
Propogation of "throws" clauses in type signatures leading to potentially a lot of refactoring due to having to update method signatures can be solved via complete type inference. Haskell has a nice way of solving this for type classes (which propogate in the same way checked exceptions do in type signatures -- and can also be used to implement typed exceptions) with partial type signatures -- essentially leaving arbitrary parts of the type "blank" for the compiler to infer.
Issues with higher order functions/lambdas can be resolved via polymorphism/generics. In languages that implement checked exceptions via an effect system (like Koka) -- effect polymorphism is a particularly nice way to solve the problem.
Haskell, Koka, Purescript, and Nim are all highly functional languages that often make use of lambdas and higher order functions, and they don't have Java's issues with checked exceptions.
One could argue that Go has checked exceptions, only with less syntax sugar to deal with them than e.g. Java.
resp, err := http.Get("https://www.google.com")
if err != nil {
log.Fatal(err)
}
You have to check that exception right there or it's gone. Ironically, the one syntax sugar Go has over Java regarding exceptions is that it's much easier to simply ignore them (in Java you'd have to wrap the whole thing in a try
and then do nothing in catch
whilst in Go you just skip the if
).