learningtech
learningtech

Reputation: 33685

unhandled exception java.net.malformedurlexception

How come this code is giving me a unhandled exception java.net.malformedurlexception in java ?

String u = "http://webapi.com/demo.zip";
URL url = new URL(u);

Can someone tell me how to fix?

Upvotes: 9

Views: 19734

Answers (3)

hesham ahmed
hesham ahmed

Reputation: 143

 java.net.malformedurlexception

It means that no legal protocol could be found in a specification string or the string could not be parsed or your URL is not confirmed the spec or missing a component I think this will help you to understand URL

https://url.spec.whatwg.org/

Upvotes: 0

Zarwan
Zarwan

Reputation: 5787

Use a try catch statement to handle exceptions:

String u = "http://webapi.com/demo.zip";
try {
    URL url = new URL(u);
} catch (MalformedURLException e) {
    //do whatever you want to do if you get the exception here
}

Upvotes: 2

Gerard Reches
Gerard Reches

Reputation: 3154

You need to handle the posible exception.

Try with this:

    try {
        String u = "http://webapi.com/demo.zip";
        URL url = new URL(u);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

Upvotes: 12

Related Questions