Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making sure all exceptions are handled

Stemming from this question, is there any way to ensure that I'm handling all exceptions could throw? Something like pattern matching exhaustiveness warning from the compiler, but for exceptions. I think this would make sense...

like image 357
zoran119 Avatar asked Oct 19 '25 09:10

zoran119


1 Answers

You can catch all exceptions by way of SomeException, but this is usually a bad idea because you also end up catching asynchronous exceptions that are thrown to the thread from outside it.

In Haskell, it is frequent to use asynchronous exceptions to trigger thread cancellations from other threads, signal out of memory errors, and the like. And it is very rare that you want to catch an asynchronous exception; they are largely unrelated to the actual logic that the thread is running. But you can still use functions like bracket and finally so that resources held by the killed thread are properly released.

There are packages like safe-exceptions and unliftio which help avoid catching asynchronous exceptions by mistake, while still providing functions like catchAny that catch other types of exceptions.

like image 198
danidiaz Avatar answered Oct 22 '25 04:10

danidiaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!