I want to create an exception hierarchy and specialized exceptions would need to take more / different arguments than the base exception class.
open class MyException(
override val message: String,
): Exception(message)
data class MySpecialException(
override val message: String,
val code: Int,
): MyException("$message (with code $code)")
fun main() {
val ex = MySpecialException("message", 123)
println(ex.message)
}
When I run this, it prints
message
and the code was lost.
I expected this to work similar to Java where I could just call super(message + "(with code " + code + ")");
in the derived class' constructor.
Why does that happen and how can I pass the code in the message?