未处理的异常 java.net.malformedurlexception
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31504123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unhandled exception java.net.malformedurlexception
提问by John
How come this code is giving me a unhandled exception java.net.malformedurlexception
in java ?
这段代码怎么给了我一个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?
有人可以告诉我如何修复吗?
回答by Gerard Reches
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();
}
回答by Zarwan
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
}
回答by hesham ahmed
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