处理java异常(handling java exception)
这个问题与java异常有关,为什么有些情况下,即使异常被捕获并且没有exit()语句,程序也会退出,但是当抛出异常时? 我的代码看起来像这样
void bindProxySocket(DefaultHttpClientConnection proxyConnection, String hostName, HttpParams params) { if (!proxyConnection.isOpen()) { Socket socket = null; try { socket = new Socket(hostName, 80); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { proxyConnection.bind(socket, params); } catch(IOException e) { System.err.println ("couldn't bind socket"); e.printStackTrace(); } } }
接着
我将这种方法称为:
bindProxySocket(proxyConn, hostName, params1);
但是,程序退出了,尽管我想通过执行其他操作来处理异常,是否可以这样,因为我没有在try catch子句中包含方法调用? 如果我再次捕获异常,即使它已经在方法中,会发生什么? 以及如果我想只在发生异常时清除finally子句中的资源,否则我该怎么办?否则,我想继续执行该程序? 我猜在这种情况下,我必须包括整段代码,直到我可以用try语句清理资源,或者我可以在句柄异常声明中执行它吗? 其中一些问题是针对这个具体案例的,但我想对我所有的问题都做出全面的回答以备将来参考。 谢谢
编辑:
java.net.UnknownHostException: www.dsewew324f.com at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.<init>(Unknown Source) at java.net.Socket.<init>(Unknown Source) at homework3.Proxy.bindProxySocket(Proxy.java:666) at homework3.Proxy$3.handle(Proxy.java:220) at org.apache.http.protocol.HttpService.doService(HttpService.java:293) at org.apache.http.protocol.HttpService.handleRequest(HttpService.java:212) at homework3.Proxy.start(Proxy.java:472) at homework3.Proxy.main(Proxy.java:1282) Exception in thread "main" java.lang.IllegalArgumentException: Socket may not be null at org.apache.http.impl.DefaultHttpClientConnection.bind(DefaultHttpClientConnection.java:80) at homework3.Proxy.bindProxySocket(Proxy.java:674)
This questions is related to java exceptions, why are there some cases that when an exception is thrown the program exits even though the exception was caught and there was no exit() statement? my code looks something like this
void bindProxySocket(DefaultHttpClientConnection proxyConnection, String hostName, HttpParams params) { if (!proxyConnection.isOpen()) { Socket socket = null; try { socket = new Socket(hostName, 80); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { proxyConnection.bind(socket, params); } catch(IOException e) { System.err.println ("couldn't bind socket"); e.printStackTrace(); } } }
and then
I call this method like this:
bindProxySocket(proxyConn, hostName, params1);
but, the program exits, although I want to handle the exception by doing something else, can it be because I didn't enclose the method call within a try catch clause? what happens if I catch the exception again even though it's already in the method? and what should I do if i want to clean resources in the finally clause only if an exception occurs and otherwise I want to continue with the program? I am guessing in this case I have to include the whole piece of code until I can clean the resources with in a try statement or can I do it in the handle exception statement? some of these questions are on this specific case, but I would like to get a thorough answer to all my questions for future reference. thanks
edit:
java.net.UnknownHostException: www.dsewew324f.com at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.<init>(Unknown Source) at java.net.Socket.<init>(Unknown Source) at homework3.Proxy.bindProxySocket(Proxy.java:666) at homework3.Proxy$3.handle(Proxy.java:220) at org.apache.http.protocol.HttpService.doService(HttpService.java:293) at org.apache.http.protocol.HttpService.handleRequest(HttpService.java:212) at homework3.Proxy.start(Proxy.java:472) at homework3.Proxy.main(Proxy.java:1282) Exception in thread "main" java.lang.IllegalArgumentException: Socket may not be null at org.apache.http.impl.DefaultHttpClientConnection.bind(DefaultHttpClientConnection.java:80) at homework3.Proxy.bindProxySocket(Proxy.java:674)
原文:https://stackoverflow.com/questions/2700705
最满意答案
attachedFiles
是数组。 因此,尝试使用索引器访问数组内容resObj.attachedFiles[0].fileName // 0th index, 1st Element
访问数组中的所有元素。 感谢@Cerbus评论
for(var i = 0, l = resObj.attachedFiles.length; i < l;i++) { console.log(resObj.attachedFiles[i].fileName); }
attachedFiles
is array. So try access the array content using indexerresObj.attachedFiles[0].fileName // 0th index, 1st Element
To access all elements in the array. Thanks to @Cerbus comment
for(var i = 0, l = resObj.attachedFiles.length; i < l;i++) { console.log(resObj.attachedFiles[i].fileName); }
相关问答
更多-
电脑二级考试 是考access 还是VPF[2022-06-08]
access -
如何访问此JSON值?(How to access this JSON value?)[2022-05-29]
parseJSON可以帮助你 var obj = jQuery.parseJSON('{"name":"John"}'); alert( obj.name ); parseJSON helps you var obj = jQuery.parseJSON('{"name":"John"}'); alert( obj.name ); -
假设你的原始JSON看起来像这样: { "_buffer": "JVBERi0xLjMKJeLjz9MKMiAwIG9iago8PAovQ3JlYXRpb25EYXRlIChEOjIwMTgwMTI2MjI0ODI1LTA1JzAwJykKL01vZERhdGUgKEQ6MjAxODAxMjYyMjQ4MjUtMDUnMDAnKQovUHJvZHVjZXIgKEJDTCBlYXN5UERGIDcuMDAgXCgwMzU1XCkpCi9DcmVhdG9yIChlYXN5UERGIFNESyA3IDcuMC ...
-
您无需循环遍历每个字段 - 只需将其作为parseJSON结果的直接属性即可访问。 var obj = $.parseJSON(result); alert(obj.IsValid); i found the problem. in the $.ajax( { type: "POST", data: "myJson=" + jsonData, url: "/myURL", success: ...
-
为什么我无法访问json(Why I can't access as json)[2022-11-04]
Javascript区分大小写。 您将数据类型设置为json以便jQuery知道要返回的内容并自动解析它。 但是,数据类型的正确键是dataType而不是datatype 。 所以你需要: $.ajax({ type: 'post', url: '/test.php', dataType: 'JSON', success: function(result) { alert(result[0].name); } }); Javascript is ca ... -
无法从JSON访问值(Can't access value from JSON)[2024-02-28]
attachedFiles是数组。 因此,尝试使用索引器访问数组内容 resObj.attachedFiles[0].fileName // 0th index, 1st Element 访问数组中的所有元素。 感谢@Cerbus评论 for(var i = 0, l = resObj.attachedFiles.length; i < l;i++) { console.log(resObj.attachedFiles[i].fileName); } attachedFiles is array. ... -
正如你已经说过的那样 - 你忘了定义方案。 所以下一个代码正在运行 var mongoose = require('mongoose'); mongoose.connect('mongodb://127.0.0.1:27017/so'); var Couple = mongoose.model('Couple', { reqts: Number }); var couple = new Couple({ reqts: 1469468008496 }); couple.save(function (err ...
-
对我来说,这样做 my_string=u'[{"id":1275829555,"guid":11}, {"id":1275829512,"guid" :12 }]' j_obj = json.loads(my_string) j_obj[0] 版画 {'guid': 11, 'id': 1275829555} 所以我在想你的json_string = str( json.dumps(my_string, ensure_ascii=False).encode('utf8') )你json_string = ...
-
从json访问值的反应(Access values from json in react)[2023-09-12]
你的帖子第一次没有数据,所以你需要这样的东西: import React, { Component } from 'react'; export default class Test extends Component { constructor(props) { // Pass props to the parent component super(props); // Set initial state this.state = { // ...