简体   繁体   中英

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?

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

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
}
 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM