异常处理

代码块
```package com.foreknow.exceptions;

import java.io.BufferedReader;

/**
 * 异常(Exception)处理:程序运行期间的错误,如果程序发生
 *所有异常的根父类:throwable
 * 检查性异常(checked exception)
*                   若系统运行时可能产生该类异常,则必须写出相应的处理代码,否则无法通过编译
*                非RuntimeException异常
* 非检查性异常(unchecked exception)
*                若系统运行时可能产生该类异常,则不必在程序中声明对该类异常的处理,就可以编译执行
                  RuntimeException:运行时异常
 *
 *Java中如何处理异常
 *1.自行处理  语法:try..........catch
 *2.回避处理  throws表示异常的声明,说明此方法有可能会出现异常
 *                 语法:public 返回值类型 方法名() throws 异常类型{}
 *                 
 * 注意:1.throws与throw的区别    2.final与finally的区别             
 * @author Administrator
 *
 */
public class Test {
    public void m1() throws Exception{
        String[] array={"tom","lisa","iu","abc"};
        for (int i = 0; i <= array.length; i++) {
            System.out.println(array[i]);
        }
    }
    public static void main(String[] args) {
        Test test=new Test();
        try {
            test.m1();
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("处理异常.......");
        }
        
        try {
            //1.出现了运行时异常
            //
            //3.对数据库进行操作
        } catch (NullPointerException e) {
            // TODO: handle exception
        }catch (Exception e) {
            // TODO: handle exception
        }finally {
            //无论try中有没有异常都执行finally中的代码
            //释放数据库连接的资源
        }
        
        
        
    }
//      try {
//          //有可能产生异常的代码
//          String[] array={"tom","lisa","iu","abc"};
//          for (int i = 0; i <= array.length; i++) {
//              System.out.println(array[i]);
//          }
//      } catch (ArrayIndexOutOfBoundsException e) {
//          // TODO: handle exception
//          System.out.println("捕获异常信息......");
//          System.out.println(e.getMessage());
//      }
//      
//      System.out.println("===========");
//  }

}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容