AgileApps Support Wiki Pre Release

Difference between revisions of "Java Error Handling"

From AgileApps Support Wiki
imported>Aeric
imported>Aeric
Line 11: Line 11:
# In a logged error message, include the method name (to find the message rapidly).
# In a logged error message, include the method name (to find the message rapidly).
# When catching an unexpected exception, display the exception's class name.<br>That's generally more indicative than the message embedded in the exception
# When catching an unexpected exception, display the exception's class name.<br>That's generally more indicative than the message embedded in the exception
# All calls to platform functions need to be in a try…catch block.
# All calls to platform functions need to be in a try-catch block.
# All calls to methods that invoke a platform function need to be in a try-catch block.
# All calls to methods that invoke a platform function need to be in a try-catch block.
# However, the operations in the catch-block are different, in those three cases:{{TBD|Merge these into the above}}
# In a method that is invoked directly from the platform, the rule is to LOG, SHOW, and THROW:
#:a. In the catch block for a method that is called from the platform:
#::<syntaxhighlight lang="java" enclose="div">
#::<syntaxhighlight lang="java" enclose="div">
// LOG, THROW, and SHOW
// LOG, SHOW, and THROW
String msg = "Unexpected exception in methodName()";
String msg = "Unexpected exception in methodName()";
log(msg + ":\n" + e.getClass().getName() ); //Sometimes helpful: + "\n" + e.getMessage() ); 
log(msg + ":\n" + e.getClass().getName() );  
show(msg + " - see debug log");     
show(msg + " - see debug log");     
throw e; // Roll back the current transaction
</syntaxhighlight>
</syntaxhighlight>
#:b. In the catch block inside a method that is called by your code:
#:
#: In a catch block inside a method that is called by your code, the rule is to LOG and THROW:
#::<syntaxhighlight lang="java" enclose="div">
#::<syntaxhighlight lang="java" enclose="div">
// LOG and THROW
// LOG and THROW
Line 28: Line 29:
throw e;
throw e;
</syntaxhighlight>
</syntaxhighlight>
#:c. In the catch block surrounding the call you make to that method:
#:
# In the catch block surrounding the call you make to that method, the rule is to SHOW:
#::<syntaxhighlight lang="java" enclose="div">
#::<syntaxhighlight lang="java" enclose="div">
// SHOW
// SHOW
Line 34: Line 36:
return;
return;
</syntaxhighlight>
</syntaxhighlight>
#:d. Outside of a catch block, use [[Functions.throwError]]:
#:
# Outside of a catch block, the rule is to THROW using [[Functions.throwError]] to generate an exception that interrupts processing and rolls back the current transaction:
#::<syntaxhighlight lang="java" enclose="div">
#::<syntaxhighlight lang="java" enclose="div">
// THROW
// THROW
Functions.throwError("msg");
Functions.throwError("msg");
</syntaxhighlight>
</syntaxhighlight>

Revision as of 23:42, 12 November 2014

The Java Class Template embodies the error handling principles explained here. To do so, it uses the following tools:

Note: Only one message is displayed, when the code returns to the platform. Multiple calls are concatenated.
  • Functions.throwError - Raise an exception to discontinue processing and roll back the current transaction.

The goal of error handling is identify which error occurred, where it happened, and (ideally) what data was present at the time. The principles elucidated below help to achieve those goals. (You can call Functions.throwError to get a stack trace, but it generally doesn't help very much, because the trace is almost entirely devoted to the sequence of platform calls that got to your code. You're more interested in the steps your program followed. Following these steps gives you that information.)

Error-Handling Principles
  1. Use the class name as "category" label when calling Logger.info (to ).
  2. In a logged error message, include the method name (to find the message rapidly).
  3. When catching an unexpected exception, display the exception's class name.
    That's generally more indicative than the message embedded in the exception
  4. All calls to platform functions need to be in a try-catch block.
  5. All calls to methods that invoke a platform function need to be in a try-catch block.
  6. In a method that is invoked directly from the platform, the rule is to LOG, SHOW, and THROW:
    <syntaxhighlight lang="java" enclose="div">

// LOG, SHOW, and THROW String msg = "Unexpected exception in methodName()"; log(msg + ":\n" + e.getClass().getName() ); show(msg + " - see debug log"); throw e; // Roll back the current transaction </syntaxhighlight>

  1. In a catch block inside a method that is called by your code, the rule is to LOG and THROW:
    <syntaxhighlight lang="java" enclose="div">

// LOG and THROW String msg = "Unexpected exception in methodName()"; log(msg + ":\n" + e.getClass().getName() ); //+ "\n" + e.getMessage() ); throw e; </syntaxhighlight>

  1. In the catch block surrounding the call you make to that method, the rule is to SHOW:
    <syntaxhighlight lang="java" enclose="div">

// SHOW show("Error in getActivities() - see debug log"); return; </syntaxhighlight>

  1. Outside of a catch block, the rule is to THROW using Functions.throwError to generate an exception that interrupts processing and rolls back the current transaction:
    <syntaxhighlight lang="java" enclose="div">

// THROW Functions.throwError("msg"); </syntaxhighlight>