Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unhandled exception java.net.malformedurlexception

Tags:

java

url

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?

like image 574
John Avatar asked Jul 19 '15 17:07

John


2 Answers

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();
    }
like image 143
Gerard Reches Avatar answered Oct 08 '22 12:10

Gerard Reches


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
}
like image 32
Zarwan Avatar answered Oct 08 '22 11:10

Zarwan