如果任何网站使用java代码不可用,如何引发异常
我正在使用java。 我有一个url作为输入。我正在尝试使用java代码打开url。 我正在使用:如果任何网站使用java代码不可用,如何引发异常,java,Java,我正在使用java。 我有一个url作为输入。我正在尝试使用java代码打开url。 我正在使用: URL url=new URL("http://doctorwho.time-and-space.co.uk/index.php"); URLConnection conn=url.openConnection(); InputStream in=conn.getInputStream(); 在这里我传递了一个链接作为输入,但是这个网站不可用。在这里我想抛出一个 打开此url
URL url=new URL("http://doctorwho.time-and-space.co.uk/index.php");
URLConnection conn=url.openConnection();
InputStream in=conn.getInputStream();
在这里我传递了一个链接作为输入,但是这个网站不可用。在这里我想抛出一个 打开此url时发生异常,但它没有引发任何异常,它正在正确执行。
请帮助我,如果网站不可用,如何捕获此异常。实际上,您的所有行都会引发异常:
java.net.URL.openConnection()抛出IOException
java.net.URLConnection.getInputStream()抛出IOException
java.net.URLConnection.getInputStream()抛出IOException
您应该一个接一个地处理这些错误,如果遇到其中任何一个,您应该在自己的代码中处理错误。可能会抛出另一个异常,停止执行,任何你想要的。可能您在这个块周围有一个很大的try-catch(异常e),您应该去掉它。实际上,您的所有行都会抛出一个异常:
java.net.URL.openConnection()抛出IOException
java.net.URLConnection.getInputStream()抛出IOException
java.net.URLConnection.getInputStream()抛出IOException
您应该一个接一个地处理这些错误,如果遇到其中任何一个,您应该在自己的代码中处理错误。可能会抛出另一个异常,停止执行,任何你想要的。可能您在这个块周围有一个很大的try-catch(异常e),您应该去掉它。如果您想抛出一个异常,而不是在函数中处理它,请使用
抛出,不要使用try-catch
:
public void foo() throws IOException
{
URL url=new URL("http://doctorwho.time-and-space.co.uk/index.php");
URLConnection conn=url.openConnection();
InputStream in=conn.getInputStream();
//...
}
如果要抛出异常,而不在函数中处理它,请使用抛出
,不要使用尝试捕获
:
public void foo() throws IOException
{
URL url=new URL("http://doctorwho.time-and-space.co.uk/index.php");
URLConnection conn=url.openConnection();
InputStream in=conn.getInputStream();
//...
}
您可以将IOException包装到自己的文件中
public void fireURL(String pathToFireParam) throws CustomException
{
try{
URL url=new URL(pathToFireParam);
URLConnection conn=url.openConnection();
InputStream in=conn.getInputStream();
} catch(IOException ioexc){
throw new CustomException("Unavailable: "+ioexc.getMessage(),ioexc);
}
}
您可以将IOException包装到自己的文件中
public void fireURL(String pathToFireParam) throws CustomException
{
try{
URL url=new URL(pathToFireParam);
URLConnection conn=url.openConnection();
InputStream in=conn.getInputStream();
} catch(IOException ioexc){
throw new CustomException("Unavailable: "+ioexc.getMessage(),ioexc);
}
}
这段代码确实抛出了一个异常:IOException
最好的方法可能是创建一个特定于您重新匹配的服务的异常类。为了让程序根据错误采取不同的操作(这可能只是显示一条好看的消息,而不是一个巨大的堆栈跟踪),您可以使用特定的异常扩展基本服务异常
所以基本例外是:
public class MyServiceException extends Exception {
private static final long serialVersionUID = 1L;
public MyServiceException(String s, Throwable throwable) {
super(s, throwable);
}
}
为所有与“stuff”相关的问题引发的异常
以及加载XML文件的代码:
private void doStufWithURL(String fileURL) throws MyServiceStufFailedException {
try {
URL url=new URL(fileURL);
URLConnection conn=url.openConnection();
InputStream in=conn.getInputStream();
// Use input Stream too...
} catch (IOException exception) {
//Don't forget to put an explanation and the cause to help while debugging.
throw new MyServiceStufFailedException("IO Error while reading " + fileURL, exception);
}
}
这段代码确实抛出了一个异常:IOException
最好的方法可能是创建一个特定于您重新匹配的服务的异常类。为了让程序根据错误采取不同的操作(这可能只是显示一条好看的消息,而不是一个巨大的堆栈跟踪),您可以使用特定的异常扩展基本服务异常
所以基本例外是:
public class MyServiceException extends Exception {
private static final long serialVersionUID = 1L;
public MyServiceException(String s, Throwable throwable) {
super(s, throwable);
}
}
为所有与“stuff”相关的问题引发的异常
以及加载XML文件的代码:
private void doStufWithURL(String fileURL) throws MyServiceStufFailedException {
try {
URL url=new URL(fileURL);
URLConnection conn=url.openConnection();
InputStream in=conn.getInputStream();
// Use input Stream too...
} catch (IOException exception) {
//Don't forget to put an explanation and the cause to help while debugging.
throw new MyServiceStufFailedException("IO Error while reading " + fileURL, exception);
}
}
“正确执行”是什么意思?当您尝试从流中读取时会发生什么?您所说的“正确执行”是什么意思?当您尝试从流中读取时会发生什么?您不应该无缘无故捕获异常!!!他可以在这里简单地捕获IO异常。这正是我的观点,请再次阅读答案:“可能你在这个块周围有一个很大的try-catch(异常e),你应该去掉它。”-意味着他不应该有一般异常捕获块。你不应该无缘无故地捕获异常!!!他可以在这里简单地捕获IO异常。这正是我的观点,请再次阅读答案:“可能你在这个块周围有一个很大的try-catch(异常e),你应该去掉它。”-意味着他不应该有一般的异常捕获块。