本文整理汇总了Java中java.lang.Thread.UncaughtExceptionHandler类的典型用法代码示例。如果您正苦于以下问题:Java UncaughtExceptionHandler类的具体用法?Java UncaughtExceptionHandler怎么用?Java UncaughtExceptionHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
UncaughtExceptionHandler类属于java.lang.Thread包,在下文中一共展示了UncaughtExceptionHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: uncaughtException
import java.lang.Thread.UncaughtExceptionHandler; //导入依赖的package包/类
@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);
}
}
示例2: newThread
import java.lang.Thread.UncaughtExceptionHandler; //导入依赖的package包/类
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setDaemon(this.daemon);
// If there is no specified name for thread, it will auto detect using the invoker classname instead.
// Notice that auto detect may cause some performance overhead
String prefix = this.name;
if (StringUtils.isBlank(prefix)) {
prefix = getInvoker(2);
}
thread.setName(prefix + "-" + getSequence(prefix));
// no specified uncaughtExceptionHandler, just do logging.
if (this.uncaughtExceptionHandler != null) {
thread.setUncaughtExceptionHandler(this.uncaughtExceptionHandler);
} else {
thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
LOGGER.error("unhandled exception in thread: " + t.getId() + ":" + t.getName(), e);
}
});
}
return thread;
}
示例3: startWriter
import java.lang.Thread.UncaughtExceptionHandler; //导入依赖的package包/类
protected synchronized void startWriter ()
{
if ( this.disposed )
{
logger.warn ( "We are disposed. Not starting writer" );
return;
}
this.writerThread = new Thread ( "BufferingStorageDao" ) {
@Override
public void run ()
{
writer ();
}
};
this.writerThread.start ();
this.writerThread.setUncaughtExceptionHandler ( new UncaughtExceptionHandler () {
@Override
public void uncaughtException ( final Thread t, final Throwable e )
{
logger.error ( "Writer thread failed. Restarting ...", e );
startWriter ();
}
} );
}
示例4: initSentryOnCurrentThread
import java.lang.Thread.UncaughtExceptionHandler; //导入依赖的package包/类
public static void initSentryOnCurrentThread() {
Sentry.init("https://2eb0fc30f86a4871a85755ecdde11679:[email protected]/252970");
Sentry.getContext().setUser(new UserBuilder().setUsername("RoboTricker").build());
Sentry.getContext().addTag("thread", Thread.currentThread().getName());
Sentry.getContext().addTag("version", TransportPipes.instance.getDescription().getVersion());
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
Sentry.capture(e);
}
});
}
示例5: run
import java.lang.Thread.UncaughtExceptionHandler; //导入依赖的package包/类
@Override
public final void run()
{
try
{
_r.run();
}
catch (Throwable e)
{
final Thread t = Thread.currentThread();
final UncaughtExceptionHandler h = t.getUncaughtExceptionHandler();
if (h != null)
{
h.uncaughtException(t, e);
}
}
}
示例6: resurrect
import java.lang.Thread.UncaughtExceptionHandler; //导入依赖的package包/类
protected void resurrect(Thread t, Throwable e, @Nullable UncaughtExceptionHandler handler) {
if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
//handle the first crash of the main looper
fixRenderingAndDispatchCrash(t, e, handler);
while (true) {
try {
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
} catch (Throwable followUpExceptions) {
//at this point, we never enter the "uncaught" exception again, because our looper is already protected
print(e);
fixRenderingAndDispatchCrash(t, e, handler);
}
}
} else {
//not the main thread, nothing to revive, but for the sake of consistency we treat it the same
fixRenderingAndDispatchCrash(t, e, handler);
}
}
示例7: fixRenderingAndDispatchCrash
import java.lang.Thread.UncaughtExceptionHandler; //导入依赖的package包/类
protected void fixRenderingAndDispatchCrash(Thread t, Throwable e, @Nullable UncaughtExceptionHandler handler) {
Thread otherThread = new Thread() {
@Override
public void run() {
mMainHandler.post(() -> {
//invoke the given handler
if (handler != null) {
handler.uncaughtException(t, e);
}
//dispatch, when possible
Scope appScope = ContextScope.getScope(mAppContext);
if (appScope != null) {
recursiveFixUI(appScope);
recursiveDispatch(appScope, t, e);
}
});
}
};
otherThread.setName("pacemaker");
otherThread.start();
}
示例8: AppCrashHandler
import java.lang.Thread.UncaughtExceptionHandler; //导入依赖的package包/类
private AppCrashHandler(Context context) {
this.context = context;
// get default
uncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
// install
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, final Throwable ex) {
// save log
saveException(ex, true);
// uncaught
uncaughtExceptionHandler.uncaughtException(thread, ex);
}
});
}
示例9: appSetup
import java.lang.Thread.UncaughtExceptionHandler; //导入依赖的package包/类
/**
* Do some common setup for all applications at startup
* @param name the application name used for Java logging and database logging
*/
public static void appSetup(String name)
{
// Set our platform wide L&F
System.setProperty("swing.defaultlaf", "javax.swing.plaf.nimbus.NimbusLookAndFeel");
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
defaults.put("Table.gridColor", new Color(140,140,140));
defaults.put("Table.showGrid", true);
// Set the program name which is used by PostgresqlDatabase to identify the app in logs
System.setProperty("program.name", name);
// Start with a fresh root set at warning
Logger root = LogManager.getLogManager().getLogger("");
Formatter format = new SingleLineFormatter();
root.setLevel(Level.WARNING);
for(Handler handler : root.getHandlers()) {
root.removeHandler(handler);
}
// Set prefs levels before windows preference load barfs useless data on the user
Logger.getLogger("java.util.prefs").setLevel(Level.SEVERE);
// postgres JDBC spits out a lot of data even though we catch the exception
Logger.getLogger("org.postgresql.jdbc").setLevel(Level.OFF);
Logger.getLogger("org.postgresql.Driver").setLevel(Level.OFF);
// Add console handler if running in debug mode
if (Prefs.isDebug()) {
ConsoleHandler ch = new ConsoleHandler();
ch.setLevel(Level.ALL);
ch.setFormatter(format);
root.addHandler(ch);
}
// For our own logs, we can set super fine level or info depending on if debug mode and attach dialogs to those
Logger applog = Logger.getLogger("org.wwscc");
applog.setLevel(Prefs.isDebug() ? Level.FINEST : Level.INFO);
applog.addHandler(new AlertHandler());
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
applog.log(Level.WARNING, String.format("\bUncaughtException in %s: %s", t, e), e);
}});
try {
File logdir = Prefs.getLogDirectory().toFile();
if (!logdir.exists())
if (!logdir.mkdirs())
throw new IOException("Can't create log directory " + logdir);
FileHandler fh = new FileHandler(new File(logdir, name+".%g.log").getAbsolutePath(), 1000000, 10, true);
fh.setFormatter(format);
fh.setLevel(Level.ALL);
root.addHandler(fh);
} catch (IOException ioe) {
JOptionPane.showMessageDialog(FocusManager.getCurrentManager().getActiveWindow(),
"Unable to enable logging to file: " + ioe, "Log Error", JOptionPane.ERROR_MESSAGE);
}
// force the initialization of IdGenerator on another thread so app can start now without an odd delay later
new Thread() {
public void run() {
IdGenerator.generateId();
}
}.start();
}
示例10: makeCommonPool
import java.lang.Thread.UncaughtExceptionHandler; //导入依赖的package包/类
/**
* Creates and returns the common pool, respecting user settings specified
* via system properties.
*/
private static ForkJoinPool makeCommonPool() {
int parallelism = -1;
ForkJoinWorkerThreadFactory factory = defaultForkJoinWorkerThreadFactory;
UncaughtExceptionHandler handler = null;
try { // ignore exceptions in accessing/parsing properties
String pp = System.getProperty("java.util.concurrent.ForkJoinPool.common.parallelism");
String fp = System.getProperty("java.util.concurrent.ForkJoinPool.common.threadFactory");
String hp = System.getProperty("java.util.concurrent.ForkJoinPool.common.exceptionHandler");
if (pp != null)
parallelism = Integer.parseInt(pp);
if (fp != null)
factory = ((ForkJoinWorkerThreadFactory) ClassLoader.getSystemClassLoader().loadClass(fp)
.newInstance());
if (hp != null)
handler = ((UncaughtExceptionHandler) ClassLoader.getSystemClassLoader().loadClass(hp).newInstance());
} catch (Exception ignore) {
}
if (parallelism < 0 && // default 1 less than #cores
(parallelism = Runtime.getRuntime().availableProcessors() - 1) < 0)
parallelism = 0;
if (parallelism > MAX_CAP)
parallelism = MAX_CAP;
return new ForkJoinPool(parallelism, factory, handler, LIFO_QUEUE, "ForkJoinPool.commonPool-worker-");
}
示例11: newDaemonThreadFactory
import java.lang.Thread.UncaughtExceptionHandler; //导入依赖的package包/类
/**
* Get a named {@link ThreadFactory} that just builds daemon threads.
* @param prefix name prefix for all threads created from the factory
* @param handler unhandles exception handler to set for all threads
* @return a thread factory that creates named, daemon threads with
* the supplied exception handler and normal priority
*/
public static ThreadFactory newDaemonThreadFactory(final String prefix,
final UncaughtExceptionHandler handler) {
final ThreadFactory namedFactory = getNamedThreadFactory(prefix);
return new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = namedFactory.newThread(r);
if (handler != null) {
t.setUncaughtExceptionHandler(handler);
} else {
t.setUncaughtExceptionHandler(LOGGING_EXCEPTION_HANDLER);
}
if (!t.isDaemon()) {
t.setDaemon(true);
}
if (t.getPriority() != Thread.NORM_PRIORITY) {
t.setPriority(Thread.NORM_PRIORITY);
}
return t;
}
};
}
示例12: handleExceptionInternal
import java.lang.Thread.UncaughtExceptionHandler; //导入依赖的package包/类
private void handleExceptionInternal(Throwable e) {
UncaughtExceptionHandler exceptionHandler;
exceptionHandler = getExceptionHandler();
try {
if (exceptionHandler != null) {
exceptionHandler.uncaughtException(Thread.currentThread(), e);
}
} finally {
handleException(e);
}
}
示例13: uncaughtException
import java.lang.Thread.UncaughtExceptionHandler; //导入依赖的package包/类
@Override
public void uncaughtException(Thread t, Throwable e) {
UncaughtExceptionHandler delegate;
synchronized (this) {
delegate = exceptionHandler;
}
if (delegate != null) {
delegate.uncaughtException(t, e);
}
}
示例14: testExceptionHandling
import java.lang.Thread.UncaughtExceptionHandler; //导入依赖的package包/类
@Test
public void testExceptionHandling() throws InterruptedException {
MockRunLoop runLoop = new MockRunLoop();
final Semaphore semaphore = new Semaphore(0);
UncaughtExceptionHandler exceptionHandler = new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
semaphore.release();
}
};
runLoop.setExceptionHandler(exceptionHandler);
assertSame(exceptionHandler, runLoop.getExceptionHandler());
try {
assertEquals(0, runLoop.getThreadPool().getCorePoolSize());
runLoop.scheduleNow(new Runnable() {
@Override
public void run() {
throw new RuntimeException("test error");
}
});
assertEquals(1, runLoop.getThreadPool().getCorePoolSize());
semaphore.acquire();
synchronized (runLoop.errors) {
if (runLoop.errors.isEmpty()) {
runLoop.errors.wait(TestUtils.TEST_TIMEOUT_MILLIS);
}
}
assertEquals(1, runLoop.errors.size());
} finally {
runLoop.getExecutorService().shutdownNow();
}
}
示例15: newThread
import java.lang.Thread.UncaughtExceptionHandler; //导入依赖的package包/类
@Override
public Thread newThread(Runnable r) {
if (r == null) {
return null;
}
Thread thread = Executors.defaultThreadFactory().newThread(r);
thread.setUncaughtExceptionHandler(
new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
// ignore -- to prevent the test output from getting cluttered
}
});
return thread;
}