Kotlin: How to invoke super after some computation?

nz_*_*_21 0 error-handling kotlin

At present, I have a custom error defined like so:

class IsEvenError(message:String):Exception(message)

val n = 10;
if (n%2 == 0) {
   throw IsEvenError("${n} is even");
}

Run Code Online (Sandbox Code Playgroud)

The problem with this is, I have to manually write out the error message every time I want to throw it.

I want to be able to embed the error message into the class itself, so I can do something like:

throw IsEvenError(n); // this should throw an error saying "10 is even".

Run Code Online (Sandbox Code Playgroud)

How do I accomplish this?

Ada*_*hip 6

You can change your IsEvenError to accept the number instead of a string, and pass the formatted string to Exception:

class IsEvenError(number: Int) : Exception("$number is even")
fun main() : Unit = throw IsEvenError(10)
Run Code Online (Sandbox Code Playgroud)

Produces:

Exception in thread "main" IsEvenError: 10 is even
    at TestKt.main(Test.kt:2)
Run Code Online (Sandbox Code Playgroud)