Difference between revisions of "Java Error Handling"
From AgileApps Support Wiki
imported>Aeric |
imported>Aeric |
||
Line 28: | Line 28: | ||
#:c. Include the exception's class name, using <tt>e.getClass().getName()</tt>. | #:c. Include the exception's class name, using <tt>e.getClass().getName()</tt>. | ||
#:: (For a standard Java exception like ArrayIndexOutOfBoundsException, the class name will generally tell you what went wrong.) | #:: (For a standard Java exception like ArrayIndexOutOfBoundsException, the class name will generally tell you what went wrong.) | ||
# | # For an internal method that is called by your code, do the following: | ||
# | #:<syntaxhighlight lang="java" enclose="div"> | ||
try { | try { | ||
... | ... | ||
Line 39: | Line 39: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
#: | #: | ||
# | # For a ''top-level'' method that is called by the platform, add an additional call to the logger: | ||
#:<syntaxhighlight lang="java" enclose="div"> | |||
try { | try { | ||
... | ... | ||
Line 51: | Line 52: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
#: | |||
#: | #: That combination of code generates log entries like these: | ||
#:: <tt>someCallingMethod(): Exception</tt> | #:: <tt>someCallingMethod(): Exception</tt> | ||
#:: <tt>someCalledMethod(): ArrayIndexOutOfBoundsException</tt> | #:: <tt>someCalledMethod(): ArrayIndexOutOfBoundsException</tt> | ||
Line 67: | Line 68: | ||
public void debug(String msg) throws Exception { show(msg); log(msg); } | public void debug(String msg) throws Exception { show(msg); log(msg); } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{Tip|<br>Don't indent debug statements you intend to remove. Instead, start them in column 1, where they're easy to see. <br> | {{Tip|<br>Don't indent debug statements you intend to remove. Instead, start them in column 1, where they're easy to see. <br>Only indent debug statements you want to keep around for later use. Comment them out when they've served their purpose.}} |
Revision as of 00:42, 25 November 2014
The goal of error handling is identify the error that occurred, where it happened, and (ideally) what data was present at the time. The ideas presented in this section can help to achieve those goals.
Error Handling Tools
The Java Class Template embodies the error handling principles explained below. To do so, it uses the following tools:
- Logger.info - Put a text message into the Debug Log. Add "/n" (newline) to create a line break.
- None of the other tools put an entry into the log. This is the only one that does.
- Functions.showMessage - Display an HTML message onscreen. Add "<br>" to create a line break.
- Multiple calls to showMessage() are concatenated in the message buffer--but only if no Exceptions occurred.
- The contents of the message buffer are displayed when the code returns to the platform.
- Functions.throwError - Raise an exception to discontinue processing, display a message, and roll back the current transaction.
- The required message argument (Functions.throwError("some message") is displayed to the user.
- That call is equivalent to throw new Exception("some message").
- Those calls overwrite the message buffer, replacing any previous calls and any stored text from calls to showMessage. so only the last call is seen by the user.
- Whenever such a message is displayed, the Debug Log should contain detailed information on the cause. Follow the steps in the next section to be sure it does.
- Logger.info - Put a text message into the Debug Log. Add "/n" (newline) to create a line break.
- Learn more:
- To test various error conditions and see the results, use this class: ErrorHandlingTest Class.
Tip:
By all means, take advantage of the Unit Test Framework to identify and fix bugs before your users see them.
Error Handling Principles
- All calls to platform functions and standard Java functions need to be in a try-catch block. (Otherwise, a standard Java exception would be ignored.)
- Calls to Functions.showMessage are useful in the normal flow of code, but not in a catch-block.
(You have to re-throw an exception to be sure it is seen. But when you re-throw it, the message it contains is the only thing the user sees.) - Use showMessage() calls to put breadcrumbs in a block of code works, therefore, as long as no exceptions occur. Putting those messages in the Debug Log is more reliable. (To get the best of both worlds, do both!)
- A standard Java stack trace is of little value, since it is almost entirely the sequence of platform calls that got to your code. You're more interested in the steps your program followed. To get that information, catch every exception and add the name of the current method to the log, along with the exception's class name:
- a. Call Logger.info. Use the class name as the "category" label (2nd parameter).
- b. Include the method name in the message.
- c. Include the exception's class name, using e.getClass().getName().
- (For a standard Java exception like ArrayIndexOutOfBoundsException, the class name will generally tell you what went wrong.)
- For an internal method that is called by your code, do the following:
try { ... } catch (Exception e) { Logger.info(e.getMessage(), "YourClass"); msg = "yourMethod(): "+e.getClass().getName(); Functions.throwError(msg); }
- For a top-level method that is called by the platform, add an additional call to the logger:
try { ... } catch (Exception e) { Logger.info(e.getMessage(), "YourClass"); msg = "yourMethod(): "+e.getClass().getName(); Logger.info( msg, "YourClass" ); Functions.throwError(msg); }
- That combination of code generates log entries like these:
- someCallingMethod(): Exception
- someCalledMethod(): ArrayIndexOutOfBoundsException
- In code that is outside of a catch block (for example, when a call worked but you got back an unexpected value), generate an exception to interrupt processing and roll back the current transaction:
// THROW THE ERROR String msg = ""Error <doing something> in yourMethod()"; Functions.throwError(msg);
- The debug method in the Java Class Template is designed for tracing code execution. If the code runs properly, you see the trace messages onscreen. If not, they are still in the Debug Log.
public void debug(String msg) throws Exception { show(msg); log(msg); }