is it possible to do this more elegant? Let these ...
# announcements
s
is it possible to do this more elegant? Let these perhaps be generated, instead of having to write them all out
s
Alas, not that I know. But you could do something like this:
Copy code
class MyException @JvmOverloads constructor(
    cause: Throwable? = null,
    message: String? = cause?.toString(),
    enableSuppression: Boolean = true,
    writableStrackTrace: Boolean = true
)Exception(message, cause, enableSuppression, writableStrackTrace)
Where the
@JvmOverloads
guarantees Java compatibility.
s
thanks! but this still isn't ideal It causes a problem when I don't put the message = there. It would ask for a Throwable instance
b
because JvmOverloads generates constructor per default parameters prefix. In case above it generates four constructors () (throwable) (throwable, message) (throwable, message, enableSupression)
so calling KotlinPluginException("message without named argument") is not working because there is no
(message)
constructor
you could swap message and throwable, but then there will be no
(throwable)
constructor. So you need just add one more constructor by hands:
constructor(message: String) : this(null, message)
t
passing null as the cause in the constructor breaks the initCause function as well. not sure if you care