Lesson 38. Errors in Haskell and the Either type
After reading lesson 38, you’ll be able to
- Throw errors by using the error function
- Understand the dangers of throwing errors
- Use Maybe as a method for handling errors
- Handle more sophisticated errors with the Either type
Most of what makes Haskell so powerful is based on the language being safe, predictable, and reliable. Although Haskell reduces or eliminates many problems, errors are an unavoidable part of real-world programming. In this lesson, you’ll learn how to think about handling errors in Haskell. The traditional approach of throwing an exception is frowned upon in Haskell, as this makes it easy to have runtime errors the compiler can’t catch. Although Haskell does allow you to throw errors, there are better ways to solve many problems that come up in your programs. You’ve already spent a lot of time with one of these methods: using the Maybe type. The trouble with Maybe is that you don’t have a lot of options for communicating what went wrong. Haskell provides a more powerful type, Either, that lets you use any value you’d like to provide information about an error.