https://kotlinlang.org logo
Title
d

david-wg2

09/05/2018, 9:26 AM
is there anything built in to simplify snippets like this?
val myValue = try { 
	getValue() 
} catch (e: Exception) { 
	throw MyException()
}
s

spand

09/05/2018, 9:27 AM
Seems like a bad pattern in any case
g

gildor

09/05/2018, 9:27 AM
yes, this code really smells
d

david-wg2

09/05/2018, 9:28 AM
how so?
g

gildor

09/05/2018, 9:28 AM
you can do something similar with Try monad and mapping Failure case, but nothing in stdlib
This code completely ignores original exception, so in case of error you cannot understand what is happened
throw MyException(e)
is better
you can write own function if use exception wrapping very often
d

david-wg2

09/05/2018, 9:29 AM
it's intended to be used for deserializing json and throwing an exception that will be mapped by the web server
g

gildor

09/05/2018, 9:30 AM
But what is the reason to lose cause?
d

david-wg2

09/05/2018, 9:32 AM
the main reason deserialization can fail is if the input is bad, i don't really care why, i just want to throw a BadRequestException
ideally it shouldn't throw exceptions at all, but it does, so it has to be dealt with
g

gildor

09/05/2018, 9:35 AM
It’s kinda strange, I still don’t see any reasons to do that in your case and yeah, this approach is really smells. Don’t forget that deserialization can fail not only because bad input, but you catch all exceptions But your question was about somethign standard, no, there is no such thing in stdlib, you can easily write
d

david-wg2

09/05/2018, 9:39 AM
thanks, i'll do that