https://kotlinlang.org logo
#announcements
Title
# announcements
d

diesieben07

09/03/2017, 12:15 PM
Yes, it's about which exception it throws.
require
throws
IllegalArgumentException
, which makes no sense here, the arguments passed in to my methods were perfectly fine.
i

ilya.gorbunov

09/03/2017, 4:42 PM
So, what exception do you want this function to throw?
d

diesieben07

09/03/2017, 6:11 PM
In Guava they have a custom class that extends
RuntimeException
, which is the most reasonable thing you can do, I think.
i

ilya.gorbunov

09/03/2017, 9:09 PM
To me it's more like an
AssertionError
— when the returned value doesn't meet the function contract.
d

diesieben07

09/03/2017, 9:34 PM
Hm, true. However I am not sure if it's "correct" or "a good idea" to re-use
AssertionError
here, since that seems to be only for the
assert
keyword.
d

dagguh

09/05/2017, 6:39 AM
I’m curious, why is the exception type important at all? Especially in a super-generic method like
require
. Do you have a exception-type-specific try-catch logic? Limit the
try
scope and you’ll know what failed on your abstraction level. Or use return values instead of data-carrying exceptions. Do you want the exception to explain the error? Let the exception message and stacktrace do it for you.
d

diesieben07

09/05/2017, 1:37 PM
The exception type is not that important, a generic
Exception
would do. It's just that
IllegalArgumentException
and
IlegalStateException
are not appropriate, just like
IOException
isn't.
👍 1