Live data from Hacker News

The Trouble with Checked Exceptions (2003)

artima.com

31–35 of 35 posts

Re: The Trouble with Checked Exceptions (2003)

#31
post #30

Earlier quoted context omitted.

> Java’s exceptions are pretty much the exact same sum type as in Rust, just with added syntactic sugar. Except Java doesn’t actually return a result object that can be assigned to a variable by default.

And in case of exceptions, why would that be useful? (For other things, I agree that sum types are cool, and fortunately are supported in Java through sealed classes)

I don’t think sealed classes offer exhaustive pattern matching. Instead you have an if-else ladder of instanceof checks. This is not the same as most union type implementations.

Re: The Trouble with Checked Exceptions (2003)

#32
post #30

Earlier quoted context omitted.

And in case of exceptions, why would that be useful? (For other things, I agree that sum types are cool, and fortunately are supported in Java through sealed classes)

I don’t think sealed classes offer exhaustive pattern matching. Instead you have an if-else ladder of instanceof checks. This is not the same as most union type implementations.

They do with switch expressions. It is only type based for now, but deconstructs are coming with full blown pattern matching a la Haskell.

Re: The Trouble with Checked Exceptions (2003)

#33

> Bill Venners: But aren't you breaking their code in that case anyway, even in a language without checked exceptions? If the new version of foo is going to throw a new exception that clients should think about handling, isn't their code broken just by the fact that they didn't expect that exception when they wrote the code? > Anders Hejlsberg: No, because in a lot of cases, people don't care. They're not going to ha…

I think exceptions are a not what you should use for regular error conditions that you can handle with the caller. ML descendants are great for that. Sum types with pattern matching to make sure the caller handle the regular error conditions, and exceptions for "catastrophic" things. This with some tooling to show you where exceptions can bubble would be perfet for me.

Re: The Trouble with Checked Exceptions (2003)

#34

An old but goodie. While Hejlsberg touches on a fair amount of good points in this discussion, I’ve never agreed with the end result. He went on to make Typescript - making sure that JS has types, so when making method calls you could have the compiler tell you when you did something wrong. Again, errors/exceptions did not get a throw clause, making it impossible to model the error state that the language provides. Y…

> Again, errors/exceptions did not get a throw clause, making it impossible to model the error state that the language provides. You can describe, in detail, the data for successful run, but are unable to describe the data for any error state. If the goal was to make the system more sound, I have a hard time seeing why the error state is not taken into account.

I think the idea is that exceptions are too hard to handle without going too far from JS, so you're supposed to handle errors Go-style, but instead of [result, error] you can return result | error thanks to union types. TS is here to statically type JS, but it can't check for exceptions, so pushing people to use them would greatly reduce the value of the tool.

I've said this in another comment, but I really think sum/union types can give you 90% of the benefits of checked exceptions. In the article, `FileNotFoundException` is mentionned. That's something that could be handle with the function returning File | FileNotFound. You can achieve this with sealed classes in C# and Java, but it sounds painful to do this every time. Like someone else said, with checked exceptions you have ad-hoc unions, instead of having to create them yourself with sealed class. Since you have neither sum types, nor unions in Java or C#, checked exceptions in Java are your best tool to handle your case.

Re: The Trouble with Checked Exceptions (2003)

#35
post #23
post #15

Earlier quoted context omitted.

But in languages supporting this you do know that it's either A or B and you can use match expressions to deal with both cases. Usually these are the languages with more focus on type safety, like OCaml, Haskell and Rust.

To be fair, in OCaml, Haskell or Rust the type "A | B" also had to be declared beforehand.

Not exactly. For example, in Rust, you can do unions for traits: https://play.rust-lang.org/?version=stable&mode=debug&editio.... OCaml has the same system for its objects, where objects are structurally typed.
Post reply on HN