Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Exception Best Practices

Handling exceptions properly can be challenging, especially in Haskell where asynchronous exceptions can interrupt code execution at any moment. This article covers best practices for exception handling in Haskell, focusing on several key goals. However, achieving all three goals simultaneously can be tricky, and they may even conflict at times.

There are two main categories of exceptions in Haskell: synchronous exceptions, which occur due to program execution, and asynchronous exceptions, which can happen at any time during execution. Synchronous exceptions are typically thrown using throwIO and caught using catch. Asynchronous exceptions, on the other hand, can be raised by throwing an exception with throwTo, which allows exceptions to be sent to arbitrary threads.

The Exception class is used to define exceptions, and it is often more convenient to use handle, which is just catch with its arguments flipped. Before throwing an exception, throwIO wraps it in the SomeException envelope, which carries dynamic typing information and exception annotations. This information is used by catch to ensure that only exceptions of the desired type are caught.

Asynchronous exceptions are particularly useful in Haskell, as they allow for interruptible code execution. This can be seen in the example of reading the first line of a file. While the handler should not run if an exception is thrown synchronously, it can run if an asynchronous exception is triggered. This is demonstrated in the code example provided, where the handler does not run as expected due to an asynchronous exception.

To prevent this issue, the code can be modified to temporarily prevent asynchronous exceptions from being raised using the mask function. Once the code is safely within an exception handler, asynchronous exceptions can be re-enabled. This pattern is so common in Haskell that it has its own combinator called bracket, which takes three arguments: one to acquire a resource, one to release it again, and the body that uses the resource.

However, there is a downside to using the bracket combinator. The exception handler no longer gets access to the exception that occurred. In cases where logging or additional information about the exception is needed, such as the position in the file where the exception was thrown, a more advanced version of the bracket combinator called generalBracket can be used. This allows the handler to be informed of how the body terminated and can also produce a result of its own.

Using a blanket exception handler that catches all exceptions, including both synchronous and asynchronous exceptions, can lead to issues. Some synchronous exceptions may be transient, while others may not be, such as a file being locked versus an invalid path. Additionally, asynchronous exceptions like timeouts or user interrupts should almost always be honored and not retried. A variation of the keepTrying function only retries when the file is locked, demonstrating a more targeted approach to exception handling.

Another issue arises when a recursive call to exception handling occurs within the exception handler itself. This can result in an unbounded number of handlers, each running under all the previous exception handlers installed. To avoid this, the return value of the exception handler should indicate whether or not to retry, allowing the retry decision to be made outside the scope of the exception handler.

In some cases, a thread can be spawned to catch all exceptions and then display the exception details using a function like anythingToString from the recover-rtti package. This approach provides a more detailed view of the exception structure, bypassing any kind of Show instance. The outermost wrapper is SomeException, with dynamic typing information and an empty list of exception annotations. The innermost construct is then displayed.

Rewritten by urgent.news; may contain errors — check the original.

Source: well-typed.com

Read the original at well-typed.com →

Report a problem with this story

More in Tech

More from Friday 27 November →