# Bug: gradle swallows Exceptions in init script

**URL:** <https://discuss.gradle.org/t/bug-gradle-swallows-exceptions-in-init-script/2269>\
**Category:** Old Forum Archive\
**Created:** [March 14, 2014, 4:21pm UTC](https://discuss.gradle.org/t/bug-gradle-swallows-exceptions-in-init-script/2269 "2014-03-14T16:21:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thibault\_Kruse1](https://avatars.discourse-cdn.com/v4/letter/t/71e660/32.png) [@Thibault\_Kruse1](https://discuss.gradle.org/u/Thibault_Kruse1)\
**Post date:** [March 14, 2014, 4:21pm UTC](https://discuss.gradle.org/t/bug-gradle-swallows-exceptions-in-init-script/2269/1 "2014-03-14T16:21:00Z")

</div>

To reproduce:

create build.gradle:

```gradle
apply plugin: "java"

```

create init.gradle:

```gradle
useLogger(new CustomEventLogger())
  class CustomEventLogger extends BuildAdapter implements TaskExecutionListener {
      public void beforeExecute(Task task) {
        println "foo"
        throw new RuntimeException("foo")
        println("bar")
    }
      public void afterExecute(Task task, TaskState state) {
        println()
    }
      public void buildFinished(BuildResult result) {
        println 'build completed'
    }
}

```

Produces: $ gradle build -I init.gradle

foo build completed

Should produce: Some sign of exception happening

Even --stacktrace and --debug do not show exception

Seen on gradle 1.9 and 1.11

---

<div class="post-metadata">

**Author:** ![Thibault\_Kruse1](https://avatars.discourse-cdn.com/v4/letter/t/71e660/32.png) [@Thibault\_Kruse1](https://discuss.gradle.org/u/Thibault_Kruse1)\
**Post date:** [March 14, 2014, 4:25pm UTC](https://discuss.gradle.org/t/bug-gradle-swallows-exceptions-in-init-script/2269/2 "2014-03-14T16:25:00Z")

</div>

Ah, sorry. Should be more specific. Using a custom Logger swallows all Exception reporting.

The code uses the example from [http://www.gradle.org/docs/current/userguide/logging.html](http://www.gradle.org/docs/current/userguide/logging.html)

Maybe there is a better way to do custom logging without swallowing Exceptions.

---

<div class="post-metadata">

**Author:** ![Thibault\_Kruse1](https://avatars.discourse-cdn.com/v4/letter/t/71e660/32.png) [@Thibault\_Kruse1](https://discuss.gradle.org/u/Thibault_Kruse1)\
**Post date:** [March 14, 2014, 4:31pm UTC](https://discuss.gradle.org/t/bug-gradle-swallows-exceptions-in-init-script/2269/3 "2014-03-14T16:31:00Z")

</div>

And to be even more specific, this happens if the Listener passed to useLogger() implements BuildListener.

So at least I suggest quickly removing BuildAdapter from the example in the docs along with the buildFinished() method. Then the example works to start with.

---

<div class="post-metadata">

**Author:** ![Thibault\_Kruse1](https://avatars.discourse-cdn.com/v4/letter/t/71e660/32.png) [@Thibault\_Kruse1](https://discuss.gradle.org/u/Thibault_Kruse1)\
**Post date:** [March 14, 2014, 4:56pm UTC](https://discuss.gradle.org/t/bug-gradle-swallows-exceptions-in-init-script/2269/4 "2014-03-14T16:56:00Z")

</div>

Also, BTW: What I wanted to do was to add logging of the time the tasks finished, to get an idea of why my builds on Jenkins take so long. However, I also do not want to loose the standard gradle outputs like SKIPPED, UP-TO-DATE, and so on, and I do not wish to reimplement the logging.

Would be nice if the documentation example rather showed how to do that, how to extend TaskExecutionLogger in a viable way maybe.

---

<div class="post-metadata">

**Author:** ![luke\_daley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/luke_daley/32/9890_2.png) [@luke\_daley](https://discuss.gradle.org/u/luke_daley)\
**Post date:** [March 17, 2014, 2:01am UTC](https://discuss.gradle.org/t/bug-gradle-swallows-exceptions-in-init-script/2269/5 "2014-03-17T02:01:00Z")

</div>

Hi Thibault,

I’m just about to start looking into this, but quickly I just wanted to point out: [http://www.gradle.org/docs/current/userguide/tutorial\_gradle\_command\_line.html#sec:profiling\_build](http://www.gradle.org/docs/current/userguide/tutorial_gradle_command_line.html#sec:profiling_build)

---

<div class="post-metadata">

**Author:** ![luke\_daley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/luke_daley/32/9890_2.png) [@luke\_daley](https://discuss.gradle.org/u/luke_daley)\
**Post date:** [March 17, 2014, 2:14am UTC](https://discuss.gradle.org/t/bug-gradle-swallows-exceptions-in-init-script/2269/6 "2014-03-17T02:14:00Z")

</div>

Hi Thibault,

I read through those docs and I think they are accurate. The first sentence of the code example you linked to says: “You can _replace_ much of Gradle’s logging UI with your own”. This feature is not designed to be used to extend Gradle’s logging, but rather replace it.

If you want to extend the logging you would just add the listener via ‘addListener()’ and not ‘useLogger()’.

---

<div class="post-metadata">

**Author:** ![Thibault\_Kruse1](https://avatars.discourse-cdn.com/v4/letter/t/71e660/32.png) [@Thibault\_Kruse1](https://discuss.gradle.org/u/Thibault_Kruse1)\
**Post date:** [March 17, 2014, 8:31am UTC](https://discuss.gradle.org/t/bug-gradle-swallows-exceptions-in-init-script/2269/7 "2014-03-17T08:31:00Z")

</div>

Hi Luke,

I can add a Listener using addListener() instead to solve my problem, thanks. ‘–profile’ is nice, but I did not find any nice jenkins integration, and I would like the information to be available in the log output. I guess I could parse the --profile output file if it exist and print that…

The logging documentation may be accurate, but it is still not helpful. It tempts users to modify their buildsystem to swallow exceptions without giving fair warning. At least the example could check the taskState in afterExecute and print a stacktrace on Exception.

regards,

Thibault

---

<div class="post-metadata">

**Author:** ![luke\_daley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/luke_daley/32/9890_2.png) [@luke\_daley](https://discuss.gradle.org/u/luke_daley)\
**Post date:** [March 17, 2014, 9:10am UTC](https://discuss.gradle.org/t/bug-gradle-swallows-exceptions-in-init-script/2269/8 "2014-03-17T09:10:00Z")

</div>

> At least the example could check the taskState in afterExecute and print a stacktrace on Exception.

That’s a good idea. I’ll make that change. Thanks.
