javapoet-dsl / com.mostlycertain.javapoetdsl / CodeBuilder / tryDecl

tryDecl

fun tryDecl(block: CodeFunc): TryFlow

Render a try-catch block.

A "catch" and "finally" can be declared by chaining calls to the returned TryFlow.

Example: tryDecl { s("foo()") }.catchDecl(IllegalArgumentException::class, "ex") { s("bar()") }.catchDecl(types(NullPointerException::class, IllegalStateException::class), "ex") { s("bar()") }.finallyDecl { s("cleanup()") }

    // -- Result --
    // try {
    //   foo();
    // } catch (final IllegalArgumentException ex) {
    //   bar();
    // } catch (final NullPointerException | IllegalStateException ex) {
    //   bar();
    // } finally {
    //   cleanup();
    // }