https://kotlinlang.org logo
Title
b

Ben Madore

09/25/2019, 7:36 PM
class PhotoLibException : RuntimeException {
    constructor(message: String, ex: Exception?): super(message, ex) {}
    constructor(message: String): super(message) {}
    constructor(ex: Exception): super(ex) {}
}
what is the appropriate way to add another property to PhotoLibException?
d

Dominaezzz

09/25/2019, 7:38 PM
class PhotoLibException(val newProp: PropType) : RuntimeException {
    constructor(message: String, ex: Exception?): super(message, ex) {}
    constructor(message: String): super(message) {}
    constructor(ex: Exception): super(ex) {}
}
b

Ben Madore

09/25/2019, 7:40 PM
open class ErrorCodeException(val code: String?) : RuntimeException{
results in an error on
RuntimeException
saying: “this type has a constructor, and thus must be initialized here”
so i tried
open class ErrorCodeException(val code: String?) : RuntimeException(){
then my secondary constructors fail with:
constructor(cause: Throwable, code: String? = null) : super(cause)
“Primary constructor expected”
d

Dominaezzz

09/25/2019, 7:42 PM
Ohhh.
I tend to pick just one constructor but I'm not what the "correct" way is.
b

Ben Madore

09/25/2019, 7:47 PM
yeah, i’m extending an exception which itself has multiple constructors, so i’m stuck there
d

Dominaezzz

09/25/2019, 7:50 PM
class PhotoLibException(val newProp: PropType, message: String, ex: Exception?) : RuntimeException(message, ex) {
}
To be honest, I don't think you need
ex
but I can't make assumptions.
b

Ben Madore

09/25/2019, 7:52 PM
those fields are optional, i still want to be able to create instance of my type with any of [message, cause, message&code, cause&code, message&cause, message&cause&code]
d

Dominaezzz

09/25/2019, 7:54 PM
class PhotoLibException() : RuntimeException {
    val newProp: PropType

    constructor(message: String, ex: Exception?, newProp: PropType): super(message, ex) { this.newProp = newProp }
    constructor(message: String, newProp: PropType): super(message) { this.newProp = newProp }
    constructor(ex: Exception, newProp: PropType): super(ex) { this.newProp = newProp }
}
b

Ben Madore

09/25/2019, 7:58 PM
class MyException : RuntimeException {
    val code: String?

    constructor(cause: Throwable, code: String? = null) : super(cause) {
        this.code = code
    }

    constructor(message: String, code: String? = null) : super(message) {
        this.code = code
    }

    constructor(message: String, cause: Throwable, code: String? = null) : super(message, cause) {
        this.code = code
    }
}