Solved

How to place a fault check for valid URL ?

Posted on 2011-03-25
6
493 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 80
JDeveloper 12c for 32 bit 4 68
Java passing arguments (type error) 2 48
how do i compare an object based on two fields 6 48
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

912 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now