Alternatives to exceptions for error handling

IGotD- nise at nise.com
Mon Nov 30 01:24:55 UTC 2020


On Sunday, 29 November 2020 at 23:20:13 UTC, Roman Kashitsyn 
wrote:
>
> I believe sum types + pattern matching + error propagation 
> sugar is a much nicer solution for errors that people might 
> care to handle. Of course, Errors don't fall into this category.

I think it can be too limiting, especially for large software 
projects. Exceptions or whatever is working underneath is usually 
better. Problem is that all sort can go wrong in a function. 
Often there is a memory allocation, which can go wrong. However, 
catching a memory allocation error almost never happens as 99% of 
the time people just assume it will work. This is where 
exceptions come handy as uncaught exceptions will abort the 
program and the programmers can start to debug the problem. One 
thing I sometimes see among badly run projects is that they catch 
all exceptions, like std::exception or (...) in C++ but the 
action will be wrong as they catch anything that they don't 
expect. So in the case of the memory allocation error, it will be 
handled the same as a bunch of other errors which is likely to be 
the wrong action.

Also for script like languages which D really can be. Exceptions 
are great as you don't care about error handling at all when you 
just mock up some script like program. If you get an exception 
the program aborts then you find the problem and fix it, then 
move on.

Uncaught exceptions often help find error that we don't expect 
and that the program aborts is also correct since we didn't know 
the error would happen and therefore didn't handle it.


More information about the Digitalmars-d mailing list