如何在单个catch块中处理所有java.net异常?

时间:2019-07-12 07:47:20

标签: java exception

我正在尝试处理java.net包中包含的异常。我经历了documentation,发现属于该程序包的12个异常,但找不到这些异常的父类。

到目前为止,我尝试过的是:

catch(Exception e)
{
    if(e instanceof org.openqa.selenium.WebDriverException)
        sendException("Problem is with the selenium web driver");
    else if(e instanceof java.net.Exception) //I need help with this line
        sendException("There is some problem with the network");
}

尝试此操作后,出现以下错误消息

  

无法解析类java.net.Exception

如何捕获java.net异常?

6 个答案:

答案 0 :(得分:1)

java.net.Exception不存在,java.lang.Exception不存在。

要检查软件包是否为java.net,请使用e.getClass()。getPackage():

catch(Exception e)
{
    if(e instanceof org.openqa.selenium.WebDriverException)
        sendException("Problem is with the selenium web driver");
    else if(e.getClass().getPackage().startsWith("java.net"))
        sendException("There is some problem with the network");
}

答案 1 :(得分:1)

java.net.Exception不存在,因此您无法以这种方式从特定程序包中捕获类的异常。
认为网络异常总是以java.net开头是错误的。
某些网络异常不是以java.net开头,并且异常也可以由另一个异常包装。例如,java.net.SocketException可以用java.lang.IllegalStateException包装。
您将不会处理它,因为这不是java.net异常。

使用instanceOf处理异常似乎也没有帮助。
catch子句中指定特定的异常就足够了。
还请注意,异常是不言自明的,并且应在其状态中包含所有相关信息:您无需像执行操作那样将异常映射到文本消息。

您要发送/记录的是利用与之相关的异常类/消息/原因/ stracktrace。您所做的只是对异常的解释,这很容易出错。

这样做:

catch(Exception e){
    sendException(e); 
}

public void sendException(Exception e){
   // exploit e.getClass(), e.getMessage() or e.getStackTrace()
   ...
}

请注意,在某些情况下,您希望捕获某些特定的异常,因为您需要对它们进行特定的处理:

catch(WebDriverException e){
     processWebDriverException(e);
}
catch(Exception e){
     processAnyOtherException(e);
}

答案 2 :(得分:1)

您可以捕获如下java.net异常:

try {

}
catch (
    BindException 
    | ConnectException 
    | HttpRetryException
    | MalformedURLException
    | NoRouteToHostException
    | PortUnreachableException
    | ProtocolException
    | SocketException
    | SocketTimeoutException
    | UnknownHostException
    | UnknownServiceException
    | URISyntaxException
    e
){
    // do stuff with e
}

但是,如果要在以后以不同方式处理每个异常之后调用instanceof,那又有什么意义呢?

在这种情况下,您应该使用不同的捕获块。

答案 3 :(得分:1)

首先,您无法抓到java.net.Exception的原因。 Exception类位于java.lang中,您也不应该例外。

那你应该抓住什么?

不幸的是,没有适用于所有联网异常的超类型,也没有不联网的其他类型。但是事实证明,大多数网络异常都以java.io.IOException作为祖先。

(有一个java.net.*并非源自IOException的异常,即java.net.URISyntaxException。但这是未经检查的异常,它并不表示“网络问题” )

因此一种方法是捕获IOException。您可以通过使用反射来获取异常类的程序包名称来进一步完善(参见Correctly merging a feature branch using TortoiseGit);例如

} catch (org.openqa.selenium.WebDriverException e) {
    sendException("Problem is with the selenium web driver");
} catch (IOException e) {
    if (e.getClass().getPackage().startsWith("java.net"))
        sendException("There is some problem with the network");
    else {
        throw e;  // Or diagnose differently
    }
}

答案 4 :(得分:0)

Example

Try{

Code where your exception occuring

}

Catch(Exception e){
 e.printStackTrace();
}

答案 5 :(得分:0)

如果没有公共的超类,则无法在单个子句中捕获这些异常。您可能想查看一下这篇文章,但是我同意您的看法,如果您必须指定12个例外情况,那不是一件好事:

Can I catch multiple Java exceptions in the same catch clause?

相关问题