okhttp internal server error url错误_Node.JS编程入门52:捕获错误之“未捕获的异常

造成Node程序崩溃的,几乎都是“未捕获的异常”。

当一个“未捕获的异常”出现时,Node会默认的终止进程的执行。

其实process.on()方法可以捕获进程级的异常,如:

var http = require("http");

var server = http.createServer(function(req,res){

response.end("Hello JShaman.com");

});

server.listen(8000);

process.on("uncaughtException",function(err){

console.error(err.message,err.stack);

})

执行、用浏览器发起访问,这时会报错:

e7adfeae1c493e5f68d577d57ea9b09b.png

但程序并没有报错。

如果不使用process.on()捕获这个异常,那么:

var http = require("http");

var server = http.createServer(function(req,res){

response.end("Hello JShaman.com");

});

server.listen(8000);

显然,当出现错误时,程序会退出:

53d8884b1bbf0993411182251283f0ea.png

可是,用process.on()捕获,并使程序不退出,真的好吗?

答案是:不好!

为什么?

因为会造成资源泄漏、内存泄漏。

参考资料:

105c3001e4229a4acbec6061c70f3008.png

也就是说,一旦发生这种错误,程序是必然要崩的。

没有立刻退出,也会因为资源泄漏,造成内存耗尽等原因而不久后退出。

正确的做法是:

var http = require("http");

var server = http.createServer(function(req,res){

response.end("Hello JShaman.com");

});

server.listen(8000);

process.on("uncaughtException",function(err){

console.error(err.message,err.stack);

server.close();

setTimeout(process.exit(),3000);

})

即:处理该处理的,然后退出。

ee7523613e2d3ff59fb687de46baa527.png

再然后呢,自然是找到问题原因,调试并解决问题。

方法?如何调试?

使用“域”:

var http = require("http");

var domain = require("domain");

var d = domain.create();

d.run(function(){

var server = http.createServer(function(req,res){

d.on("error",function(er){

res.statusCode = 500;

res.end("internal server error");

server.close();

setTimeout(process.exit(),3000,1);

})

response.end("Hello JShaman.com");

});

server.listen(8000);

});

使用域,可以让发代码运行在一个沙盒中、可以更好的监控部分代码。如果我们怀疑哪部分代码有问题,可以使用这种方法,监控、将错误提示给用户端,当然也可以输出日志协助我们找到问题点、解决问题。

991e1302f0a04dd05269e86b8e2b8984.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
E/AndroidRuntime: FATAL EXCEPTION: Thread-18 Process: com.example.read, PID: 22568 java.lang.RuntimeException: java.net.UnknownServiceException: CLEARTEXT communication to 192.168.210.113 not permitted by network security policy at com.example.read.upload_serverActivity$1.run(upload_serverActivity.java:111) at java.lang.Thread.run(Thread.java:920) Caused by: java.net.UnknownServiceException: CLEARTEXT communication to 192.168.210.113 not permitted by network security policy at okhttp3.internal.connection.RealRoutePlanner.planConnectToRoute$okhttp(RealRoutePlanner.kt:195) at okhttp3.internal.connection.RealRoutePlanner.planConnect(RealRoutePlanner.kt:152) at okhttp3.internal.connection.RealRoutePlanner.plan(RealRoutePlanner.kt:67) at okhttp3.internal.connection.FastFallbackExchangeFinder.launchTcpConnect(FastFallbackExchangeFinder.kt:118) at okhttp3.internal.connection.FastFallbackExchangeFinder.find(FastFallbackExchangeFinder.kt:62) at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:267) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:84) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:65) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:205) at okhttp3.internal.connection.RealCall.execute(RealCall.kt:158) at com.example.read.upload_serverActivity$1.run(upload_serverActivity.java:106) at java.lang.Thread.run(Thread.java:920) 怎么解决
最新发布
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值