|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.openide.ErrorManager
A system of managing, annotating, and classifying errors and log messages.
Rather than printing raw exceptions to the console, or popping up dialog boxes with exceptions, or implementing custom debug or logging facililities, code may use the error manager to access logging and error-reporting in a higher-level fashion. Standard error manager implementations can then provide generic ways of customizing logging levels for different components, and so on.
Especially important is the attaching of annotations such as stack traces to
exceptions, permitting you to throw an exception of a type permitted by your
API signature while safely encapsulating the root cause of the problem (in terms
of other nested exceptions). Code should use notify(Throwable)
rather
than directly printing caught exceptions, to make sure nested annotations are not lost.
Also localized messages may be annotated to exceptions so that code which can deal with a caught exception with a user-visible UI can display a polite and helpful message. Messages with no localized annotation can be handled in a default way while the details are reserved for the log file.
There may be zero or more instances of ErrorManager
in Lookup.getDefault()
.
All will be delegated to, or a simple backup implementation is available if there are none.
How to...
If it might be an important error (show the user):
try { foo.doSomething(); } catch (IOException ioe) { ErrorManager.getDefault().notify(ioe); }
If it is not very important but should be sent to the log file:
try { foo.doSomething(); } catch (IOException ioe) { ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); }
If it is the normal outcome of a user action and there is no need to show stack traces to the user:
try { foo.doSomething(); } catch (IOException ioe) { // Also WARNING or ERROR: ErrorManager.getDefault().notify(ErrorManager.USER, ioe); }
You can also specify the severity when you are creating the
exception (by annotating it), rather than relying on the notifier
to do this. In that case, if the notifier just use the plain form
of notify
(i.e. UNKNOWN
severity), the
annotated severity is used.
public void doSomething() throws IOException { try { doSomethingElse(); } catch (IllegalArgumentException iae) { IOException ioe = new IOException("did not work: " + iae); ErrorManager.getDefault().annotate(ioe, iae); throw ioe; } }
You can also just use JDK 1.4 causes:
public void doSomething() throws IOException { try { doSomethingElse(); } catch (IllegalArgumentException iae) { IOException ioe = new IOException("did not work: " + iae); ioe.initCause(iae); throw ioe; } } // ... try { foo.doSomething(); } catch (IOException ioe) { // The IllegalArgumentException is still available here: ErrorManager.getDefault().notify(ioe); }
public void doSomething(File f) throws IOException { if (!f.isFile()) { IOException e = new IOException("Not a file: " + f); // NOI18N // For what the user actually sees: ErrorManager.getDefault().annotate(e, NbBundle.getMessage(This.class, "EXC_not_a_file", f)); throw e; } }
You can also add the message when the exception is caught rather than when it is thrown. You could even have one piece of code throw an exception, another annotate it, and yet another notify it.
IOException all = null; for (int i = 0; i < things.length; i++) { try { things[i].process(); } catch (ThingProcessingException e) { if (all == null) { all = new IOException("Could not process one or more things"); // NOI18N } ErrorManager.getDefault().annotate(all, e); } } if (all != null) { throw all; }
public void doSomething(String arg) { if (arg.length() == 0) { ErrorManager.getDefault().log(ErrorManager.WARNING, "Warning: doSomething called on empty string"); return; } // ... }
package org.netbeans.modules.foo; class FooModule { public static final ErrorManager ERR = ErrorManager.getDefault().getInstance("org.netbeans.modules.foo"); } // ... class Something { public void doSomething(String arg) { if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) { ERR.log("Called doSomething with arg " + arg); } } }
Nested Class Summary | |
static interface |
ErrorManager.Annotation
Annotation that can be attached to an error. |
Field Summary | |
static int |
ERROR
Serious problem, application may be crippled. |
static int |
EXCEPTION
Something went wrong, though it can be recovered. |
static int |
INFORMATIONAL
Message that would be useful for tracing events but which need not be a problem. |
static int |
UNKNOWN
Undefined severity. |
static int |
USER
Something the user should be aware of. |
static int |
WARNING
Something went wrong in the software, but it is continuing and the user need not be bothered. |
Constructor Summary | |
ErrorManager()
|
Method Summary | |
abstract Throwable |
annotate(Throwable t,
int severity,
String message,
String localizedMessage,
Throwable stackTrace,
Date date)
Annotates given exception with given values. |
Throwable |
annotate(Throwable t,
String localizedMessage)
Annotates given exception with given values. |
Throwable |
annotate(Throwable target,
Throwable t)
Annotates target exception with given exception. |
abstract Throwable |
attachAnnotations(Throwable t,
ErrorManager.Annotation[] arr)
Associates annotations with an exception. |
Throwable |
copyAnnotation(Throwable t,
Throwable copyFrom)
Deprecated. Now does the same thing as annotate(Throwable,Throwable)
except marks the annotation UNKNOWN severity. Otherwise
you used to have inadvertent data loss when copyFrom
had annotations of its own: the subannotations were kept but the
main stack trace in copyFrom was discarded. In practice
you usually want to keep all of copyFrom ; if for some
reason you just want to keep annotations, please do so explicitly
using findAnnotations(java.lang.Throwable) and attachAnnotations(java.lang.Throwable, org.openide.ErrorManager.Annotation[]) . |
abstract ErrorManager.Annotation[] |
findAnnotations(Throwable t)
Finds annotations associated with a given exception. |
static ErrorManager |
getDefault()
Getter for the default version of error manager. |
abstract ErrorManager |
getInstance(String name)
Returns an instance with given name. |
boolean |
isLoggable(int severity)
Test whether a messages with given severity will be logged in advance. |
boolean |
isNotifiable(int severity)
Test whether a throwable, if notified at the given
level, will actually be displayed in any way (even to a log file etc.). |
abstract void |
log(int severity,
String s)
Logs the message to a file and (possibly) tells the user. |
void |
log(String s)
Logs the message to log file and (possibly) tells the user. |
abstract void |
notify(int severity,
Throwable t)
Prints the exception to the log file and (possibly) notifies the user. |
void |
notify(Throwable t)
Prints the exception to the log file and (possibly) notifies the user. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int UNKNOWN
notify(int, Throwable)
and annotate(Throwable, int, String, String, Throwable, Date)
.
public static final int INFORMATIONAL
public static final int WARNING
public static final int USER
public static final int EXCEPTION
public static final int ERROR
Constructor Detail |
public ErrorManager()
Method Detail |
public static ErrorManager getDefault()
public abstract Throwable attachAnnotations(Throwable t, ErrorManager.Annotation[] arr)
t
- the exceptionarr
- array of annotations (or null
)
t
(as a convenience)public abstract ErrorManager.Annotation[] findAnnotations(Throwable t)
t
- the exception
null
public abstract Throwable annotate(Throwable t, int severity, String message, String localizedMessage, Throwable stackTrace, Date date)
t
- the exceptionseverity
- integer describing severity, e.g. EXCEPTION
message
- message to attach to the exception or null
localizedMessage
- localized message for the user or null
stackTrace
- exception representing the stack trace or null
date
- date or null
t
(as a convenience)public abstract void notify(int severity, Throwable t)
UNKNOWN
severity means that the error manager should automatically
select an appropriate severity level, for example based on the contents of
annotations in the throwable.
severity
- the severity to be applied to the exception (overrides default), e.g. EXCEPTION
t
- the exception to notifypublic final void notify(Throwable t)
t
- the exception to notifyUNKNOWN
,
notify(int, Throwable)
public abstract void log(int severity, String s)
severity
- the severity to be applied (overrides default)s
- the log messagepublic final void log(String s)
s
- the log messagepublic boolean isLoggable(int severity)
The default implementation just returns true. Subclasses should override to be more precise - treat this method as abstract.
severity
- the severity to check, e.g. EXCEPTION
false
if the next call to log(int,String)
with this severity will
discard the messagepublic boolean isNotifiable(int severity)
notified
at the given
level, will actually be displayed in any way (even to a log file etc.).
If not, there is no point in constructing it.
This method is distinct from isLoggable(int)
because an error manager
implementation may choose to notify stack traces at a level where it would
not log messages. See issue #24056 for justification.
The default implementation just calls isLoggable(int)
. Subclasses
should override to be more precise - treat this method as abstract.
severity
- a notification severity
public abstract ErrorManager getInstance(String name)
By convention, you can name error managers the same as packages (or classes)
they are designed to report information from.
For example, org.netbeans.modules.mymodule.ComplicatedParser
.
The error manager implementation should provide some way of configuring e.g. the logging level for error managers of different names. For example, in the basic NetBeans core implementation, you can define a system property with the same name as the future error manager (or a package prefix of it) whose value is the numeric logging level (e.g. -J-Dorg.netbeans.modules.mymodule.ComplicatedParser=0 to log everything). Other implementations may have quite different ways of configuring the error managers.
name
- the desired identifying name
public final Throwable annotate(Throwable t, String localizedMessage)
t
- the exceptionlocalizedMessage
- localized message for the user or null
t
(as a convenience)public final Throwable annotate(Throwable target, Throwable t)
Consider using Throwable.initCause(java.lang.Throwable)
instead; this
will be correctly reported by the NetBeans error manager, and
also works properly with Throwable.printStackTrace()
.
target
- the exception to be annotatedt
- the exception that will be added
target
(as a convenience)public final Throwable copyAnnotation(Throwable t, Throwable copyFrom)
annotate(Throwable,Throwable)
except marks the annotation UNKNOWN
severity. Otherwise
you used to have inadvertent data loss when copyFrom
had annotations of its own: the subannotations were kept but the
main stack trace in copyFrom
was discarded. In practice
you usually want to keep all of copyFrom
; if for some
reason you just want to keep annotations, please do so explicitly
using findAnnotations(java.lang.Throwable)
and attachAnnotations(java.lang.Throwable, org.openide.ErrorManager.Annotation[])
.
t
- the exception to annotatecopyFrom
- exception to take annotations from
t
(as a convenience)
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |