# Catching and logging uncaught exceptions from classes invoked in tasks

**URL:** <https://discuss.gradle.org/t/catching-and-logging-uncaught-exceptions-from-classes-invoked-in-tasks/6616>\
**Category:** Old Forum Archive\
**Created:** [October 25, 2013, 3:26pm UTC](https://discuss.gradle.org/t/catching-and-logging-uncaught-exceptions-from-classes-invoked-in-tasks/6616 "2013-10-25T15:26:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thag](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/thag/32/357_2.png) [@Thag](https://discuss.gradle.org/u/Thag)\
**Post date:** [October 25, 2013, 3:26pm UTC](https://discuss.gradle.org/t/catching-and-logging-uncaught-exceptions-from-classes-invoked-in-tasks/6616/1 "2013-10-25T15:26:00Z")

</div>

As far as I can tell, if you don’t catch and log your exceptions/throwables inside the class invoked from a gradle task, gradle’s probably doesn’t to show it to you and you’re left with the very unhelpful: Execution failed for task ‘:parse’.

The ‘–stacktrace’ option appears to apply only to exceptions thrown from within the Gradle framework itself, as it doesn’t seem to help in the case where the task application code that is doing the throwing.

The only workaround I can see is to wrap all application classes that are called from gradle tasks with something like:

```gradle
public void someMethodCalledFromGradleThatCouldThrow() throws ProcessingException {
try {
    //
    //
Do things.
    //
    } catch (ProcessingException e) {
        LOG.error("Exception caught while processing", e);
        e.printStackTrace();
        throw e;
    } catch (Throwable e) {
        LOG.error("Exception in gradle task", e);
        e.printStackTrace();
        throw new ProcessingException(e);
    }
}

```

Is there something I’m missing to show application stack traces from gradle tasks?

This seems like a really common use case and adding the above to everything is very non-DRY.

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [October 25, 2013, 5:06pm UTC](https://discuss.gradle.org/t/catching-and-logging-uncaught-exceptions-from-classes-invoked-in-tasks/6616/2 "2013-10-25T17:06:00Z")

</div>

The exception type and message are always shown (for 'GradleException’s, only the message is shown). And ‘–stacktrace’ always shows the stack trace, no matter which exception and where it’s thrown. That’s the intended behavior, and it’s also what I see.

---

<div class="post-metadata">

**Author:** ![Thag](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/thag/32/357_2.png) [@Thag](https://discuss.gradle.org/u/Thag)\
**Post date:** [October 31, 2013, 4:10am UTC](https://discuss.gradle.org/t/catching-and-logging-uncaught-exceptions-from-classes-invoked-in-tasks/6616/3 "2013-10-31T04:10:00Z")

</div>

While it does work for test cases, we’re seeing some conditions that consistently do not provide any stack information and kill off the gradle daemon.

Here’s the tail of the output from a task that failed with no stacktrace.

Without using the daemon: gradle -b /opt/nim/parserV4/build.gradle run -d

20:52:01.570 [ERROR] [org.gradle.api.Project] org.gradle.api.internal.LocationAwareException: Script ‘/opt/nim/parserV4/workflows/replica.gradle’ line: 33 Execution failed for task ‘:transformPdfs’. 20:52:01.571 [DEBUG] [org.gradle.logging.internal.DefaultLoggingConfigurer] Finished configuring with level: DEBUG, configurers: [org.gradle.logging.internal.OutputEventRenderer@5c244f19, org.gradle.logging.internal.logback.LogbackLoggingConfigurer@53c52191, org.gradle.logging.internal.JavaUtilLoggingConfigurer@331ee18e]

Here’s the replica.gradle task that is failing:

```gradle
task transformPdfs(dependsOn:parse, description: "Transform PDFs to raster images") {
 inputs.dir parseDir
 outputs.dir generatedDir
 outputs.files("${parseDir}/${ParserConstants.CFG_FILENAME}")
 doLast {
  generatedDir.deleteDir()
  generatedDir.mkdirs()
  PdfConverter pdf = new PdfConverter(project.ext.dataset, "${workflow}", cbCfg) // line 33
  pdf.processPdfs(generatedDir)
 }
}

```

With --daemon arg, the daemon terminates with a gradle internal stack trace:

21:05:27.320 [ERROR] [org.gradle.BuildExceptionReporter] FAILURE: Build failed with an exception. 21:05:27.320 [ERROR] [org.gradle.BuildExceptionReporter] 21:05:27.321 [ERROR] [org.gradle.BuildExceptionReporter] \* What went wrong: 21:05:27.321 [ERROR] [org.gradle.BuildExceptionReporter] Gradle build daemon disappeared unexpectedly (it may have been stopped, killed or may have crashed) 21:05:27.323 [ERROR] [org.gradle.BuildExceptionReporter] 21:05:27.324 [ERROR] [org.gradle.BuildExceptionReporter] \* Exception is: 21:05:27.325 [ERROR] [org.gradle.BuildExceptionReporter] org.gradle.launcher.daemon.client.DaemonDisappearedException: Gradle build daemon disappeared unexpectedly (it may have been stopped, killed or may have crashed) 21:05:27.325 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.daemon.client.DaemonClient.handleDaemonDisappearance(DaemonClient.java:232) 21:05:27.326 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.daemon.client.DaemonClient.monitorBuild(DaemonClient.java:210) 21:05:27.326 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.daemon.client.DaemonClient.executeBuild(DaemonClient.java:182) 21:05:27.326 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.daemon.client.DaemonClient.execute(DaemonClient.java:151) 21:05:27.326 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.daemon.client.DaemonClient.execute(DaemonClient.java:74) 21:05:27.327 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:50) 21:05:27.327 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.api.internal.Actions$RunnableActionAdapter.execute(Actions.java:171) 21:05:27.327 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:201) 21:05:27.327 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:174) 21:05:27.328 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:170) 21:05:27.328 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:139) 21:05:27.328 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33) 21:05:27.329 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22) 21:05:27.329 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.Main.doAction(Main.java:46) 21:05:27.329 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45) 21:05:27.329 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.Main.main(Main.java:37) 21:05:27.330 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:50) 21:05:27.330 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:32) 21:05:27.330 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.GradleMain.main(GradleMain.java:23)

I can post more complete information if needed.

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [October 31, 2013, 7:12am UTC](https://discuss.gradle.org/t/catching-and-logging-uncaught-exceptions-from-classes-invoked-in-tasks/6616/4 "2013-10-31T07:12:00Z")

</div>

To make progress, we’d need a self-contained reproducible example.

---

<div class="post-metadata">

**Author:** ![Thag](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/thag/32/357_2.png) [@Thag](https://discuss.gradle.org/u/Thag)\
**Post date:** [October 31, 2013, 4:36pm UTC](https://discuss.gradle.org/t/catching-and-logging-uncaught-exceptions-from-classes-invoked-in-tasks/6616/5 "2013-10-31T16:36:00Z")

</div>

I’ll work on putting together a test case.

One thought here is that gradle only print stacktraces for Exception but not Throwable. Things like NPEs are not shown.

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [October 31, 2013, 4:44pm UTC](https://discuss.gradle.org/t/catching-and-logging-uncaught-exceptions-from-classes-invoked-in-tasks/6616/6 "2013-10-31T16:44:00Z")

</div>

Cannot reproduce. NPEs show just fine for me, both in the configuration and the execution phase.
