How to place a fault check for valid URL ?

Posted on 2011-03-25
Last Modified: 2012-05-11
How can I put a fault check for the following line that it will get a valid URL as an input? in this case the input is ReportPath.

WebConversation wc = new WebConversation();
WebResponse wr = wc.getResponse(ReportPath);

If I don't pass a valid URL then it says"

java.lang.RuntimeException: No protocol specified in URL ''

Question by:Tolgar
  • 3
  • 2
LVL 92

Accepted Solution

objects earned 500 total points
ID: 35219524
catch the MalformedURLException

try {
   URL url = new URL(ReportPath);
   // url is valid
} catch (MalformedURLException ex) {
   // url is bad
LVL 86

Expert Comment

ID: 35219569
You probably want the ability to use a file as well, so try
if(!ReportPath.startsWith("http:") && !ReportPath.startsWith("file:")) {
    ReportPath = "file:/" + ReportPath;
WebResponse wr = wc.getResponse(ReportPath);

Open in new window

LVL 92

Expert Comment

ID: 35220552
if you have a file that you want to load then use the following to convert it to a url
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

LVL 86

Expert Comment

ID: 35232803
The exception was already caught for you

Author Comment

ID: 35232864

LVL 86

Expert Comment

ID: 35232902
>>java.lang.RuntimeException: No protocol specified in URL

Means that the exception was already caught for you. My point is that it's not so much the exception as what is necessary to stop the exception happening that's at issue here

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
web services creation SOAP vs REST 5 57
servlet example issue 6 46
hibernate jars 4 45
how to debug htl and js pages 8 39
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question