简体   繁体   English

未处理的异常java.net.malformedurlexception

[英]unhandled exception java.net.malformedurlexception

How come this code is giving me a unhandled exception java.net.malformedurlexception in java ? 为什么这段代码在java中给我一个unhandled exception java.net.malformedurlexception

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: 使用try catch语句来处理异常:

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 这意味着在规范字符串中找不到合法协议或者无法解析字符串或者您的URL未确认规范或缺少组件我认为这将有助于您理解URL

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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