首页 \ 问答 \ 处理java异常(handling java exception)

处理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
更新时间:2022-09-18 12:09

最满意答案

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 indexer

resObj.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);
}

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。