我想为我的Android应用程序中的所有线程设置一个全局未捕获的异常处理程序.所以,在我的Application
子类中,我Thread.UncaughtExceptionHandler
为未捕获的异常设置了默认处理程序的实现.
Thread.setDefaultUncaughtExceptionHandler(
new DefaultExceptionHandler(this));
Run Code Online (Sandbox Code Playgroud)
在我的实现中,我试图显示一个AlertDialog
显示适当的异常消息.
但是,这似乎不起作用.无论何时,对于任何未处理的线程抛出异常,我都会获得库存操作系统默认对话框("抱歉!-Application-have-stopped-unexpectedly dialog").
为未捕获的异常设置默认处理程序的正确和理想方法是什么?
我有一个HTML文件:
<!DOCTYPE HTML>
<html lang="en-US" ng-app="Todo">
<head>
<meta charset="UTF-8">
<title>DemoAPI</title>
<meta name="viewport">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
<link rel="stylesheet" href="./Client/css/styling.css" />
<script type="text/javascript" src="core.js"></script>
</head>
Run Code Online (Sandbox Code Playgroud)
错误说:
Uncaught SyntaxError: Unexpected token < core.js: 1
Run Code Online (Sandbox Code Playgroud)
它显示了在错误<!doctype html>
的app.html
.
core.js
看起来像这样:
angular.module('Todo', [])
.controller('mainController', function($scope, $http)
{
$scope.formData = {};
// get all and show them
$http.get('/musicians')
.success(function(data) {
$scope.todos = data;
console.log(data);
})
.error(function(data) {
console.log('Error: ' + data);
});
//get with an id
$scope.getOneTodo = …
Run Code Online (Sandbox Code Playgroud) 在阅读了一些关于AppDomain.UnhandledException和Application.DispatcherUnhandledException之间差异的优秀帖子之后,似乎我应该同时处理它们.这是因为用户可以从主UI线程抛出的异常(即Application.DispatcherUnhandledException)中恢复的可能性更大.正确?
另外,我是否还应该让用户有机会继续这两个程序,或者仅仅是Application.DispatcherUnhandledException?
下面的示例代码处理AppDomain.UnhandledException和Application.DispatcherUnhandledException,并且两者都为用户提供了尝试继续的选项,尽管有异常.
[谢谢,下面的一些代码从其他答案解除]
App.xaml中
<Application x:Class="MyProgram.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="App_StartupUriEventHandler"
Exit="App_ExitEventHandler"
DispatcherUnhandledException="AppUI_DispatcherUnhandledException">
<Application.Resources>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
App.xaml.cs [编辑]
/// <summary>
/// Add dispatcher for Appdomain.UnhandledException
/// </summary>
public App()
: base()
{
this.Dispatcher.UnhandledException += OnDispatcherUnhandledException;
}
/// <summary>
/// Catch unhandled exceptions thrown on the main UI thread and allow
/// option for user to continue program.
/// The OnDispatcherUnhandledException method below for AppDomain.UnhandledException will handle all other exceptions thrown by any thread.
/// </summary>
void AppUI_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{ …
Run Code Online (Sandbox Code Playgroud) c# wpf exception-handling uncaught-exception uncaughtexceptionhandler
好的,所以我有一个问题.如果在处理HTTP请求时发生未捕获的异常,我就没有机会在http.ServerResponse对象上调用end()方法.因此,服务器永远挂起,永远不会满足请求.
这是一个例子:
var express = require('express');
var app = express.createServer();
var reqNum = 0;
app.get('/favicon.ico', function(req, res) {res.send(404);});
app.get('*', function(req, res, next) {
console.log("Request #", ++reqNum, ":", req.url);
next();
});
app.get('/error', function(req, res, next) {
throw new Error("Problem occurred");
});
app.get('/hang', function(req, res, next) {
console.log("In /hang route");
setTimeout(function() {
console.log("In /hang callback");
if(reqNum >= 3)
throw new Error("Problem occurred");
res.send("It worked!");
}, 2000);
});
process.on('uncaughtException', function(err) {
console.log("Uncaught exception!", err);
});
app.listen(8080);
Run Code Online (Sandbox Code Playgroud)
如果您访问/错误,则会发生异常,但会被捕获.用户收到错误消息 - 没问题.但是,如果我访问/挂起,服务器最终将抛出未捕获的异常并永久挂起.任何后续的/ hang请求都将挂起.
这很糟糕.有关如何解决此问题的任何建议?
是否有一种标准的方法来捕获在诸如tomcat或Jetty之类的java servlet容器内发生的未捕获的异常?我们运行了很多来自库的servlet,所以我们不能轻易地把我们放在try/catch代码上.通过提供的API将我们的Web应用程序(在Jetty中运行)中的所有未捕获的异常捕获并记录到我们的错误跟踪器中,这也是很好的方式.
请不要我只需要记录例外,无论重定向是自定义错误页面的问题都无济于事.我们通过GWT-RPC完成所有工作,因此用户永远不会看到错误页面.
我在控制台中出现了这一行,仅在Firefox中,我正在开发的JavaScript应用程序:
它似乎相对无害,但我很好奇是否有任何方法可以推断它的起源,因为它必须来自某个地方,即使它声称"未知".将整个脚本包装在try/catch块中并切换Firefox的"Pause on Exception"设置不会执行任何操作,这似乎意味着它是一个特殊的例外?我有一些想法,我的代码的哪些部分可能导致它使用Working Draft API,但我更感兴趣的是它为什么报告这种方式以及我可以做些什么.Firefox不提供更多细节吗?
我想在我的Java应用程序中定义一个应用程序级别UncaughtExceptionHandler,如果在我的应用程序的一个线程中抛出未捕获的异常,则调用该应用程序.我知道可以为一组线程(ThreadGroup)定义一个未捕获的异常,我实际上正在使用它,但我想为没有定义自己的未捕获异常处理程序的线程定义一个全局未捕获的异常,或者是没有与一组定义了默认异常处理程序的线程相关联.
所以例如我想达到这样的东西:
1° LEVEL ---> Call thread own UncaughtExceptionHandler ---> 2° LEVEL Call Thread Group UncaughtExceptionHandler ---> 3° LEVEL Call application(default) UncaughtExceptionHandler
Run Code Online (Sandbox Code Playgroud)
简单来说,我想覆盖默认的UncaughtExceptionHandler并定义我自己的处理程序而不是打印堆栈跟踪System.err
(这是默认行为).
例如,在C#.NET中,我执行类似的操作,处理应用程序的Main()方法中的未处理和线程异常事件处理程序:
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
Run Code Online (Sandbox Code Playgroud)
即使在Java中也可以完成?
如何在Java中覆盖默认的UncaughtExceptionHandler?
我的应用程序导致一个强制关闭某个地方,但我没有在我的LogCat中获得通常(且信息量很大)堆栈跟踪的致命异常,我只收到以下4行:
06-27 07:08:54.546: D/dalvikvm(14351): GC_FOR_MALLOC freed 9923 objects / 657416 bytes in 21ms
06-27 07:08:54.769: W/dalvikvm(14351): threadid=20: thread exiting with uncaught exception (group=0x4001d7f0)
06-27 07:08:54.796: W/dalvikvm(14351): threadid=21: thread exiting with uncaught exception (group=0x4001d7f0)
06-27 07:08:54.796: I/Process(14351): Sending signal. PID: 14351 SIG: 9
Run Code Online (Sandbox Code Playgroud)
这是在DEBUG模式下,LogCat上没有应用过滤器!
更新:感谢下面的@assylias,我已经能够实现:
final UncaughtExceptionHandler subclass = Thread.currentThread().getUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
Log.getStackTraceString(paramThrowable);
subclass.uncaughtException(paramThread, paramThrowable);
}
});
Run Code Online (Sandbox Code Playgroud)
这产生了这些增加的线:
06-27 08:24:47.105: D/dalvikvm(15475): GC_FOR_MALLOC freed 13865 objects / 1435952 bytes in …
Run Code Online (Sandbox Code Playgroud) 这是一个跟进的原因为什么Alexandrescu不能使用std :: uncaught_exception()在ScopeGuard11中实现SCOPE_FAIL?
我想检测是否有人MyClass
在另一个类的析构函数中创建(或者在调用堆栈中的某个地方使用了一个活动的析构函数).
class MyClass
{
public:
MyClass(){
assert(???what to put here????);
}
}
void f(){
MyClass m; //whether this asserts should be context dependant
}
class OtherClass{
~OtherClass(){
MyClass m; //this should assert
f(); //this should too;
}
}
int main()
{
MyClass m; //this should not assert
f(); //this should also not assert
}
Run Code Online (Sandbox Code Playgroud)
一次尝试可能是:
assert(!std::uncaught_exception());
Run Code Online (Sandbox Code Playgroud)
但是只有在因为异常而被调用析构函数时才会有效,而不是因为对象超出范围而被调用.
我在我的Java应用程序中使用第三方库.此第三方库在每次应用程序启动时都会抛出自定义未捕获的异常.该例外是无害的,仅用于内部第三方库的日志记录.由于未捕获此异常,因此每次启动应用程序时都会导致Eclipse IDE切换到调试透视图并挂起线程执行,以通知我该问题.我必须手动告诉Eclipse忽略这一点,并且每次都要恢复调试.这非常烦人.我无法更改第三方库以解决此问题.
有没有办法告诉Eclipse IDE忽略特定类型的未捕获异常?
我试过"Step Filtering"但是(我认为)因为自定义未捕获异常不在堆栈跟踪中,所以它不会从调试器中过滤掉.这是我第一次涉足Step Filtering,所以我可能会错误地使用它.这是一个示例堆栈跟踪.
Daemon Thread [Thread-13] (Suspended (exception CustomThirdPartyException))
ThreadPoolExecutor$Worker.run() line: not available [local variables unavailable]
Thread.run() line: not available
Run Code Online (Sandbox Code Playgroud)
编辑:
在禁用Eclipse中所有未捕获的异常后,jluzwick使用我们自己的记录器来监视未捕获的异常可能在技术上有效,但它并不理想,如果我们的记录器坏了,我们可能会错过任何东西.
mazaneicha的解决方案似乎在正确的轨道上,但我无法让它按照我想要的方式工作.这可能是由于我的用户错误.
jluzwick和mazaneicha都有可能解决这个问题,但Konstantin Komissarchik有"正确"的答案,因为这应该被推回到图书馆的创作者来修复.有时技术解决方案不正确.
java ×4
exception ×3
android ×2
javascript ×2
c# ×1
c++ ×1
debugging ×1
destructor ×1
eclipse ×1
express ×1
firefox ×1
html ×1
jetty ×1
node.js ×1
stack-trace ×1
syntax-error ×1
wpf ×1