下面列出了java.lang.Thread.UncaughtExceptionHandler#uncaughtException ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void uncaughtException(Thread t, Throwable e) {
if (!(e instanceof ThreadDeath)) {
UncaughtExceptionHandler h = Thread.getDefaultUncaughtExceptionHandler();
if (h != null) {
h.uncaughtException(t, e);
return;
}
if (e instanceof VirtualMachineError) {
// Try as hard as possible to get a stack trace from e.g. StackOverflowError
e.printStackTrace();
}
System.err.flush();
Exceptions.printStackTrace(e);
}
else {
super.uncaughtException(t, e);
}
}
/**
* Fire all ChangeListeners.
* <p>
* This should be called when the filter has fundamentally changed what
* it may accept. For example: if this filter is based on text in a text field,
* then every time that text field is changed we should call this method
*/
public void fireChangeListeners() {
for (ChangeListener changeListener : changeListeners
.toArray(new ChangeListener[changeListeners.size()])) {
try {
changeListener.stateChanged(new ChangeEvent(this));
} catch (Exception e) {
UncaughtExceptionHandler u = getUncaughtExceptionHandler();
if (u != null) {
u.uncaughtException(Thread.currentThread(), e);
} else {
e.printStackTrace();
}
}
}
}
/**
* Define custom ExceptionHandler which takes action on OutOfMemoryError.
*/
private void setExceptionHandler() {
final UncaughtExceptionHandler defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
UncaughtExceptionHandler customExceptionHandler =
new UncaughtExceptionHandler() {
@Override
public void uncaughtException(final Thread thread, final Throwable ex) {
if (ex instanceof OutOfMemoryError) {
// Store info about OutOfMemoryError
PreferenceUtil.setSharedPreferenceBoolean(R.string.key_internal_outofmemoryerror, true);
}
// re-throw critical exception further to the os
defaultExceptionHandler.uncaughtException(thread, ex);
}
};
Thread.setDefaultUncaughtExceptionHandler(customExceptionHandler);
}
private void handleExceptionInternal(Throwable e) {
UncaughtExceptionHandler exceptionHandler;
exceptionHandler = getExceptionHandler();
try {
if (exceptionHandler != null) {
exceptionHandler.uncaughtException(Thread.currentThread(), e);
}
} finally {
handleException(e);
}
}
@Override
public void uncaughtException(Thread t, Throwable e) {
try {
UncaughtExceptionHandler delegate;
synchronized (this) {
delegate = exceptionHandler;
}
if (delegate != null) {
delegate.uncaughtException(t, e);
}
} finally {
logger.error("Event handler threw an exception", e);
}
}
private void a(Thread thread, Throwable th) {
for (UncaughtExceptionHandler uncaughtException : c()) {
try {
uncaughtException.uncaughtException(thread, th);
} catch (Throwable th2) {
}
}
}
/**
* Convert a String representation to an Object
*
* @param toStringValue
* the serialized String value
* @param componentType
* the type of object that is returned.
* @param handler
* a handler to notify if something goes wrong
* @return the Object represented by the argument String.
*/
protected Object convert(String toStringValue, Class<?> componentType,
UncaughtExceptionHandler handler) {
if (String.class.equals(componentType))
return toStringValue;
if (Boolean.class.equals(componentType)
|| Boolean.TYPE.equals(componentType))
return Boolean.parseBoolean(toStringValue);
if (Integer.class.equals(componentType)
|| Integer.TYPE.equals(componentType))
return Integer.parseInt(toStringValue);
if (Short.class.equals(componentType)
|| Short.TYPE.equals(componentType))
return Short.parseShort(toStringValue);
if (Long.class.equals(componentType) || Long.TYPE.equals(componentType))
return Long.parseLong(toStringValue);
if (Character.class.equals(componentType)
|| Character.TYPE.equals(componentType))
return toStringValue.charAt(0);
if (Byte.class.equals(componentType) || Byte.TYPE.equals(componentType))
return Byte.parseByte(toStringValue);
if (Float.class.equals(componentType)
|| Float.TYPE.equals(componentType))
return Float.parseFloat(toStringValue);
if (Double.class.equals(componentType)
|| Double.TYPE.equals(componentType))
return Double.parseDouble(toStringValue);
if (BigInteger.class.equals(componentType))
return new BigInteger(toStringValue);
if (BigDecimal.class.equals(componentType))
return new BigDecimal(toStringValue);
if (File.class.equals(componentType))
return new File(toStringValue);
handler.uncaughtException(
Thread.currentThread(),
new UnsupportedEncodingException(
"ContainerProperties cannot parse "
+ componentType.getCanonicalName()));
return null;
}
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
synchronized (list) {
for (UncaughtExceptionHandler handler : list) {
handler.uncaughtException(thread, throwable);
}
}
}
static void uncaught(@NonNull Throwable error) {
Thread currentThread = Thread.currentThread();
UncaughtExceptionHandler handler = currentThread.getUncaughtExceptionHandler();
handler.uncaughtException(currentThread, error);
}