日期:2014-05-20  浏览次数:20754 次

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

------解决方案--------------------
Java code

try {

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

------解决方案--------------------
你继续把这个异常抛出,在它的上级处理掉。
public void method() throws Exception
------解决方案--------------------
Java code

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 + "...";
        }
    }

------解决方案--------------------
把异常Catch掉 然后做处理 。。。
------解决方案--------------------
StringIndexOutOfBoundsException 是一个RuntimeException异常,可不必理会程序都会继续运行
------解决方案--------------------
异常最好到一个层统一处理,哈哈

每天回贴好习惯。。。
------解决方案--------------------
先用String 接受字符串
然后判断是否为空
空则无操作
非空则转int.