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...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With