我正在开发一个Spring应用程序,并且想知道是否有一部分框架允许我以更优雅的方式执行此类操作,例如在XML中配置某些内容?
我试图在android使用时发生崩溃时重新启动应用程序Thread.UncaughtExceptionHandler.我可以使用当前活动堆栈作为新进程重新启动应用程序吗?如果是的话我该怎么办?
我创建了ac#dll来处理应用程序中所有未处理的异常.
添加
AppDomain appDomain = AppDomain.CurrentDomain;
appDomain.UnhandledException += new UnhandledExceptionEventHandler(MyErrorHandler);
Run Code Online (Sandbox Code Playgroud)
我的dll项目中的代码,添加了对我的应用程序的引用.
在调试时,如果我的应用程序抛出一个未经处理的异常,它会自动从dll中捕获并成功登录到文件.
但是当我的应用程序被部署(或直接执行我的应用程序(双击exe))时,dll无法从应用程序捕获未处理的异常.
我想在记录未处理的异常后关闭应用程序。在这里搜索后我做了以下事情:
public class MyApplication extends Application {
//uncaught exceptions
private Thread.UncaughtExceptionHandler defaultUEH;
// handler listener
private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
//logging code
//..........
//call the default exception handler
defaultUEH.uncaughtException(thread, ex);
}
};
public MyApplication() {
defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
}
}
Run Code Online (Sandbox Code Playgroud)
打电话后,defaultUEH.uncaughtException(thread, ex);我尝试打电话System.exit()( android.os.Process.killProcess(android.os.Process.myPid());甚至我发现一些帖子被告知要同时使用)。问题是我遇到黑屏,我必须使用手机任务管理器强制应用程序退出。我究竟做错了什么?
问候
我不确定为什么没有调用uncaughtException方法.
static
{
/**
* Register a logger for unhandled exceptions.
*/
Thread.UncaughtExceptionHandler globalExceptionHandler = new Thread.UncaughtExceptionHandler()
{
@Override
public void uncaughtException(Thread t, Throwable e)
{
System.out.println("handle exception."); // can also set bp here that is not hit.
}
};
Thread.setDefaultUncaughtExceptionHandler(globalExceptionHandler);
Thread.currentThread().setUncaughtExceptionHandler(globalExceptionHandler);
/**
* Register gateway listen port.
*/
try
{
// some stuff that raises an IOException
}
catch (IOException e)
{
System.out.println("Throwing exception");
throw new RuntimeException(e);
}
}
Run Code Online (Sandbox Code Playgroud)
程序输出是:
投掷例外
java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: java.io.FileNotFoundException: blah.jks
(The system …Run Code Online (Sandbox Code Playgroud) 用例:
结果:异常永远不会被捕获,并且没有跟踪/记录它。在异步系统的情况下,这是 1) 不可取的和 2) 要发现的硬和隐藏问题(例如 NPE、Runtime Exc 等)的指标。
问题:与java.lang类比/以类似方式实现CompletableFuture.UncaughtExceptionHandler机制是否可行。Thread.UncaughtExceptionHandler?如果 CompletableFuture 链没有附加 java.util.concurrent.CompletableFuture.UniExceptionally Completion,则提供 [默认] 未捕获的异常处理程序/完成。
并尝试使用“try ~ catch”进行捕获,但它不起作用。
function a(){
try{
var img1 = "==""; <#-- it occurs error -->
}catch (e) {
console.log("image error:" + e.message);
}
}
Run Code Online (Sandbox Code Playgroud)javascript error-handling try-catch uncaughtexceptionhandler