0

I have two functions that invoke a code block with a try-catch like:

fun <R: Any> executeRestCall(
    block: () -> R
): R {
    try {
        return block()
    } catch (ex: RestClientException) {
        throw IllegalArgumentException(ex.message)
    }
}


fun <R: Any> executeProcessCall(
    block: () -> R
): R {
    try {
        return block()
    } catch (ex: RuntimeException) {
        throw IllegalStateException(ex.message)
    }
}

I need to parametrize required exception. How can this function be refactored?

darth jemico
  • 605
  • 2
  • 9
  • 18
  • What do you mean by "parameterise"? Do you just want to combine the two functions into one? – Sweeper Sep 27 '21 at 15:07
  • 1
    What is the benefit of these methods? All you end up doing is erasing the stack trace – EpicPandaForce Sep 27 '21 at 15:09
  • 1
    Even ignoring the drawback about hiding the stacktrace, what exactly would be the benefit of extracting anything here? It looks like the resulting function would just be reinventing the `try-catch` block – Joffrey Sep 27 '21 at 15:10
  • @Sweeper, yes, i want to combine them – darth jemico Sep 27 '21 at 15:18
  • 1
    Does this answer your question? [How to catch many exceptions at the same time in Kotlin](https://stackoverflow.com/questions/36760489/how-to-catch-many-exceptions-at-the-same-time-in-kotlin) – Sweeper Sep 27 '21 at 15:19
  • @EpicPandaForce thanks for your point, i'll pass ex further into catch block ex. But still, i think that these two methods can be combined... – darth jemico Sep 27 '21 at 15:20
  • @Sweeper not very.. the idea is that i want to invoke it like `executeCombined() { ... some call... }` and the IllegalStateException should be thrown inside the function – darth jemico Sep 27 '21 at 15:23
  • @Joffrey the only benefit - to have a single function that handles calls with try catch – darth jemico Sep 27 '21 at 15:24
  • 1
    Does [this](https://stackoverflow.com/questions/49984075/how-to-circumvent-kotlins-restriction-type-parameter-is-forbidden-for-catch-pa) answer your question then? – Sweeper Sep 27 '21 at 15:26
  • 2
    Just call `block()` without ever using these methods, then delete these methods :D – EpicPandaForce Sep 27 '21 at 15:53
  • @bajiepka13 my point is that even if you extract that and pass the exception types to catch and throw as arguments, the resulting function is nothing more than another way of writing `try-catch` so it brings no values to the users apart from using a non-standard approach for something that's otherwise well known. – Joffrey Sep 27 '21 at 16:38
  • Also, why do you even want to wrap those exceptions in the first place? What's the "bigger" purpose for these `catch` blocks? It looks like these functions are removing useful information like the stacktrace and exception type, so I wonder why you need them in the first place. At first sight I have to admit I agree with @EpicPandaForce, just don't use those functions at all :) – Joffrey Sep 27 '21 at 16:40
  • @Joffrey I guess OP's goal is to make it more concise. But still, it doesn't make too much sense to me. It reminds me Java where we sometimes wanted to wrap checked exceptions in unchecked ones. In Kotlin it is not necessary, so if this is not for some Java interop then I don't see the point. – broot Sep 27 '21 at 16:46
  • This is not more concise, just use a try-catch – EpicPandaForce Sep 28 '21 at 08:34

1 Answers1

0

I'm not sure what you mean by parametrize

However, if you're looking for a way to indicate what exception the function will throw similar to the way throws works in Java - you can use @Throws annotation in kotlin

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-throws/

Matt
  • 186
  • 10