how to throw exception if any website is unavailable using java code
I am working with java. I have one url as a input.I am trying to open url using java code. I am using:
URL url=new URL("http://doctorwho.time-and-space.co.uk/index.php");
URLConnection conn=url.openConnection();
InputStream in=conn.getInputStream();
here I have passed one link as a input开发者_高级运维,but this website is unavailable.Here I want to throw an exception on opening this url,but it is not throwing any exception,it is executing properly. Please help me,how to catch this exception if website is unavailable.
Actually all of your lines can throw an exception:
java.net.URL.openConnection() throws IOException
java.net.URLConnection.getInputStream() throws IOException
java.net.URLConnection.getInputStream() throws IOException
You should handle these one by one, and if you encounter any of them, you should deal with the error in your own code. Maybe throw another exception, stop execution, anything you want. Probably you have a big try-catch (Exception e) around this block, which you should get rid of.
If you want to throw an exception, and not handling it in the function, use throws
, and don't use 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();
//...
}
You can wrap the IOException into your own.
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);
}
}
This code is indeed throwing an exception: IOException.
The best thing probably is to create an Exception class specific to this service that you recatch. To let the program take differents actions depending of the error (this can be just displaying a nice looking message instead of an hugly stacktrace), you can extends your base service exception with the specific exception.
So the base Exception :
public class MyServiceException extends Exception {
private static final long serialVersionUID = 1L;
public MyServiceException(String s, Throwable throwable) {
super(s, throwable);
}
}
The Exception that is thrown for all problem related to "stuff"
public class MyServiceStufFailedException extends MyServiceException {
private static final long serialVersionUID = 1L;
public MyServiceStufFailedException(String s, Throwable throwable) {
super(s, throwable);
}
}
And the code that load the XML file :
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);
}
}
精彩评论