小弟我执行一段代码,在网络上下载网页代码,有一个网址的页面不存在,虚拟机抛出了 java.lang.StringIndexOutOfBoundsException 异常。
我想让程序不理这个异常或者经过别的处理,然后继续执行循环的代码,而不影响程序的执行。
我该怎么处理这个问题,求高手指点…………
在线等待…………

解决方案 »

  1.   


    try {} catch (StringIndexOutOfBoundsException e) {
         // catch exception
    }
    // go no
      

  2.   

    你继续把这个异常抛出,在它的上级处理掉。
    public void method() throws Exception
      

  3.   


      String html = "";
    //创建URL连接
    java.net.URL url = new URL(weburl);
    URLConnection uc = url.openConnection();
    //设定字符集
    try {
    InputStreamReader read = new InputStreamReader(uc.getInputStream(),"UTF-8");
    BufferedReader br = new BufferedReader(read);
    //读取下载网页html代码保存到字符串html中
    while(true){
    String str = br.readLine();
    if(str==null)break;
    html += str;
    }
    } catch (Exception e) {
    html="页面没有找到!";
    }
    以上是部分代码!在主方法里边有一个循环在调用这段代码所在的方法!
    我的想法是跳过继续去执行循环!
      

  4.   


    public void a(String weburl) throws Exception {
    String html = "";
    // 创建URL连接
    java.net.URL url = new URL(weburl);
    URLConnection uc = url.openConnection();
    // 设定字符集
    try {
    InputStreamReader read = new InputStreamReader(uc.getInputStream(),
    "UTF-8");
    BufferedReader br = new BufferedReader(read);
    // 读取下载网页html代码保存到字符串html中
    while (true) {
    String str = br.readLine();
    if (str == null)
    break;
    html += str;
    }
    } catch (Exception e) {
    html = "页面没有找到!";
    throw e;
    }
    }

    // 主方法调用
    public void test() {
    String url = "";
    while(true) {
    try {
    a(url);
    } catch(Exception e) {

    }
    url = url + "...";
    }
    }
      

  5.   

    把异常Catch掉  然后做处理 
      

  6.   

    StringIndexOutOfBoundsException 是一个RuntimeException异常,可不必理会程序都会继续运行
      

  7.   

    先用String 接受字符串
    然后判断是否为空
    空则无操作
    非空则转int.