concept error in category kotlin

This is an excerpt from Manning's book Functional Programming with Kotlin.
Throwing an exception or halting with an error
There are many different kinds of parsing libraries. There are even several open source Kotlin parser combinator libraries available. As in the previous chapter, we’re deriving our own library from first principles partially for pedagogical purposes, and to further encourage the idea that no library is authoritative. Ours will be designed for expressiveness, by being able to parse arbitrary grammars, as well as for speed, and good error reporting. This last point is important. Whenever we run a parser on input that isn’t expected—which can happen if the input is malformed—it should generate a parse error. If there are parse errors, we want to be able to point out exactly where the error is in the input and accurately indicate its cause. Error reporting is often an afterthought in parsing libraries, but we’ll make sure that we give careful attention to it from the start.
Listing 9.9. Representing stacked errors using the ParseError data type
data class ParseError(val stack: List<Pair<Location, String>>)

This is an excerpt from Manning's book The Joy of Kotlin.
It will never throw any kind of exception. It might throw errors, such as out-of-memory errors (OOMEs) or stack-overflow errors (SOEs), but these errors mean that the code has a bug. This isn’t a situation either you, as a programmer, or the users of your API are supposed to handle (besides crashing the application, which often won’t happen automatically, and eventually fixing the bug).
In chapter 6, you learned how to deal with optional data without having to manipulate
nullreferences using theOptiondata type. As you saw, this data type is perfect for dealing with the absence of data when that isn’t the result of an error. But it’s not an efficient way to handle errors because, although it allows you to cleanly report the absence of data, it swallows the cause of the absence. All missing data is treated the same way, and it’s up to the caller to try to figure out what happened. Often, this is impossible.In this chapter, you’ll work through a variety of exercises that teach you how to handle errors and exceptions in Kotlin. One skill you’ll learn is how to represent the absence of data due to an error, which the
Optiontype doesn’t allow. We’ll first look at the problems that need solving, and then we’ll explore theEithertype and theResulttype:
To handle the case where a function could return values of two different types, such as one representing data and one representing an error, you’ll use a special type: the
Eithertype. Designing a type that can hold either anAor aBis easy. You need to slightly modify theOptiontype by changing theNonetype to make it hold a value. You’ll also change the names. The two private subclasses of theEithertype will be calledLeftandRightas shown in the following listing.Listing 7.1 The
Eithertypesealed class Either<out A, out B> { internal class Left<out A, out B>(private val value: A): Either<A, B>() { override fun toString(): String = "Left($value)" } internal class Right<out A, out B>(private val value: B) : Either<A, B>() { override fun toString(): String = "Right($value)" } companion object { fun <A, B> left(value: A): Either<A, B> = Left(value) fun <A, B> right(value: B): Either<A, B> = Right(value) } }Now you can easily use
Eitherinstead ofOptionto represent values that could be absent due to errors. You need to parameterizeEitherwith the type of your data and the type of the error.By convention, you’ll use the
Rightsubclass to represent success (which is right) and theLeftsubclass to represent error. But you won’t call the subclassWrongbecause theEithertype can be used to hold data that’s represented by one type or another, both being valid.