CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2000
    Posts
    98

    URGENT : malformed URL

    hi ... i have written a program similiar to what is attached below. the program is suppose to fetch a url of which will be specified. however, there are times when the url that i want to fetch is broken or malformed and the program returns a MALFORMED URL EXCEPTION. i would like to enquire how i can detect whether a URL link is broken or not ? because my program is suppose to fetch different URLs and that if one URL is malformed, then it will fetch another URL and not hang there ( of which is what happening now ) how do i modify my program so that it can continue to fetch other url instead of stopping when MALFORMED URL EXCEPTION occurs ???

    are they any built in boolean functions to test if an URL is malformed ???

    would appreciate all help and suggestions .... thank you and best regards.
    xilver.


    import java.lang.System;
    import java.net.*;
    import java.io.*;
    import java.util.*;
    public class URLReader {
    public static void main(String[] args) throws Exception {

    java.util.Properties prop = System.getProperties();
    prop.put("firewallSet", "true");
    prop.put("proxyHost", "proxy.ntu.edu.sg"); // replace with your proxy
    prop.put("proxyPort", "8080"); // replace with your proxy port
    System.setProperties(prop);

    URL fetch = new URL(targetURL); // target URL is fetched from a file containing a list of URLS
    BufferedReader in = new BufferedReader(
    new InputStreamReader(
    fetch.openStream()));

    PrintWriter out = new PrintWriter(
    new BufferedWriter(
    new FileWriter(
    new File("C:\\documents\\fetch.txt"))));


    String inputLine;
    while ((inputLine = in.readLine()) != null)
    out.println(inputLine);
    in.close();
    out.close();
    }
    }


  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Use try{}catch(){} to handle errors

    You need to study the try{}catch(){} statement and use that to catch the MalformedURL. Remove the throws clause from the main() method. Here's some simple code:

    URL url = null;
    try {
    url = new URL(.....);
    ... code to use the good url
    }catch(MalformedURLException mue) {
    ... code to process bad url
    }



    Norm
    Norm

  3. #3
    Join Date
    May 2001
    Posts
    594

    Re: URGENT : malformed URL

    to repeat. Never throw Exception from main. It's stupid.

    Bayard
    bayard@generationjava.com
    http://www.generationjava.com

    Brainbench MVP for Java
    http://www.brainbench.com
    Bayard
    bayard@generationjava.com
    http://www.apache.org/~bayard
    http://www.generationjava.com

    Brainbench MVP for Java
    http://www.brainbench.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured