如何捕获ExceptionInInitializerError?

时间:2016-07-03 13:34:13

标签: java static-initializer

我想安装某种全局处理程序来捕获任何ExceptionInInitializerError,当某些类加载时,这些 #include "allh.h" #define SERV_TCP_PORT_A1 5035 struct data{ __m128i num; } recva1,recva2,senda1,senda2; int main(){ int socka1,newsocka1,clengtha1; struct sockaddr_in serv_addra1,cli_addra1; char buffer[4096]; socka1=socket(AF_INET,SOCK_STREAM,0); serv_addra1.sin_family=AF_INET; serv_addra1.sin_addr.s_addr=INADDR_ANY; serv_addra1.sin_port=htons(SERV_TCP_PORT_A1); printf("\nStart"); bind(socka1,(struct sockaddr*)&serv_addra1,sizeof(serv_addra1)); printf("\nListening..."); //printf("\n"); listen(socka1,5); clengtha1=sizeof(cli_addra1); newsocka1=accept(socka1,(struct sockaddr*)&cli_addra1,&clengtha1); printf("Accepted ===== B1"); printf("\n"); srand(time(NULL)); // randomize seed //int a=5,w=8,i=0; while(1){ __m128i a,res; uint32_t *val; unsigned int a1,a2,a3,a4; a1 = rand();a2=rand();a3 = rand();a4 = rand();// a = _mm_set_epi32(a1,a2,a3,a4); read(newsocka1,&recva1,4096); printf("YO \n" ); senda1.num = res; write(newsocka1,&senda1,4096); } return 0;`enter code here` } 可能会从任何静态块抛出。目前,它将堆栈跟踪转储到stderr并退出应用程序。我想使用我的日志框架记录堆栈跟踪,然后退出应用程序。有可能吗?

2 个答案:

答案 0 :(得分:1)

看起来好像是Thread.UncaughtExceptionHandler。

answer将为您提供更多信息。

实质上,您需要尽快安装默认异常处理程序:

Picasso.with(context)
        .load(imageUrl)
        .into(img, new com.squareup.picasso.Callback() {
                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onError() {
                    }
                });

答案 1 :(得分:1)

也许你可以创建一个全局异常处理程序,只是过滤掉你的异常。

示例

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    public void uncaughtException(Thread t, Throwable e) {
        if (e instanceof ExceptionInInitializerError) {
            //Your handler code
        }
    }
});