52

Possible Duplicate:
Is it possible to detect if an exception occurred before I entered a finally block?

I have a workflow method that does things, and throws an exception if an error occurred. I want to add reporting metrics to my workflow. In the finally block below, is there any way to tell if one of the methods in the try/catch block threw an exception ?

I could add my own catch/throw code, but would prefer a cleaner solution as this is a pattern I'm reusing across my project.

@Override
public void workflowExecutor() throws Exception {
  try {
      reportStartWorkflow();
      doThis();
      doThat();
      workHarder();
  } finally {
      /**
       * Am I here because my workflow finished normally, or because a workflow method
       * threw an exception?
       */
      reportEndWorkflow(); 
  }
}
Community
  • 1
  • 1
Kevin
  • 11,521
  • 22
  • 81
  • 103
  • 2
    This question might be a duplicate of the one suggested, but it is very hard to tell, because the other question is quite admirably convoluted. The ability to determine within a finally block whether the block is executing within the normal flow of control or due to an exception having been thrown would be immensely useful because it would simplify transaction processing; alas, neither java nor C# currently offer this ability afaik. Shame. – Mike Nakis Aug 18 '14 at 17:28

3 Answers3

67

There is no automatic way provided by Java. You could use a boolean flag:

boolean success = false;
try {
  reportStartWorkflow();
  doThis();
  doThat();
  workHarder();
  success = true;
} finally {
  if (!success) System.out.println("No success");
}
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
9

Two solutions: call reportEndWorkflow twice, once in a catch block and once in the end of try:

try {
    // ...
    reportEndWorkflow("success");
} catch (MyException ex) {
    reportEndWorkflow("failure");
}

Or you can introduce a boolean variable:

boolean finished = false;
try {
    // ...
    finished = true;
} finally {
    // ...
}
Vivien Barousse
  • 20,555
  • 2
  • 63
  • 64
1

You're there because your try-block has completed execution. Whether an exception was thrown or not.

To distinguish between when an exception occur or whether your method flow execution completed successfully, you could try doing something like this:

boolean isComplete = false;
try
{
  try
  {
    reportStartWorkflow();
    doThis();
    doThat();
    workHarder();
    isComplete = true;
  }
  catch (Exception e)
  {}
}
finally
{
  if (isComplete)
  {
    // TODO: Some routine
  }
}
Bitmap
  • 12,402
  • 16
  • 64
  • 91
  • 3
    This doesn't match the OP's code. You are swallowing all exceptions and loosing the information about the cause. Unlike in the OP's case, an exception thrown in doThis() or doThat() isn't propagated outside of workFlowExecutor(). – Florian F May 25 '18 at 14:59