'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?



Solution 1:[1]

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();
    }

Solution 2:[2]

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
}

Solution 3:[3]

 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/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gerard Reches
Solution 2 Zarwan
Solution 3 timiTao