4 ms·
I am curious why anyone still thinks that exception is superior/better/desirable than a simple typed functions (like Result<T,E> or Option<T>). > You get proce
by anon-3988 3y ago
I am curious why anyone still thinks that exception is superior/better/desirable than a simple typed functions (like Result<T,E> or Option<T>).
> You get processor exceptions when you access a bad pointer, divide by 0, etc. If you don't want the program to instantly terminate, you need to be able to handle those exceptions.
The processor does a lot of other things too, all of these functions can simply be wrapped with the appropriate Result<T,E> and Option<T>. Hell, I'd argue that for a system programming language all mathematical operations should return Result<T, ArithmeticError>.
How is a try/catch better than a pattern match on the exact errors that you care about? inb4 checked exception, which is just an inferior version of algebraic data types.
- Dwedit 3y agoEverything mentioned in this post relates to API level, for code that you control. You can pick your favorite type for optional values with the possibility of an error. Exceptions, on the other hand, can come from code you don't control. Some DLL that you call into doesn't care if you like Option<T> better, it's still going to throw an exception. It could be designed to "throw" using the C++ exception syntax, or it could simply be dereferncing a null pointer or dividing by zero, causing an exception. So then the issue becomes support for catching the exceptions caused by code outside of your control. One option is to do nothing and just let the program die. The other option is to catch the exception, automatically save the user's work, then restart the program.
- anon-3988 3y agoI see, but isn't exceptions implementation specific? How does language X knows how some random binary/DLL is going to throw an exception? What kind of object does it throw? What does it look like? How big is it? How many nested exceptions are there?
- Dwedit 3y agoWindows has an ABI for exceptions, I don't know about Linux.