Solved

How to place a fault check for valid URL ?

Posted on 2011-03-25
6
494 Views
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 ''

Thanks,
0
Comment
Question by:Tolgar
  • 3
  • 2
6 Comments
 
LVL 92

Accepted Solution

by:
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
   ...
}
0
 
LVL 86

Expert Comment

by:CEHJ
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

0
 
LVL 92

Expert Comment

by:objects
ID: 35220552
if you have a file that you want to load then use the following to convert it to a url
http://helpdesk.objects.com.au/java/how-to-convert-a-file-to-a-url
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 86

Expert Comment

by:CEHJ
ID: 35232803
The exception was already caught for you
0
 

Author Comment

by:Tolgar
ID: 35232864
@ CEHJ:

How?
0
 
LVL 86

Expert Comment

by:CEHJ
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
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 90
backtracking recursion  code 19 57
maven module vs maven project 3 22
eclipse console opening separately 2 16
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

786 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