is there anything built in to simplify snippets li...
# announcements
d
is there anything built in to simplify snippets like this?
Copy code
val myValue = try { 
	getValue() 
} catch (e: Exception) { 
	throw MyException()
}
s
Seems like a bad pattern in any case
g
yes, this code really smells
d
how so?
g
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
it's intended to be used for deserializing json and throwing an exception that will be mapped by the web server
g
But what is the reason to lose cause?
d
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
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
thanks, i'll do that