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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
java set up 1 48
firstChar challenge 13 86
Eclipse Neon and jdk 1.8.0 11 72
groovy example issue 10 70
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)
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

760 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

19 Experts available now in Live!

Get 1:1 Help Now