学习笔记:java的错误和异常

错误

硬件或操作系统的错误

异常

程序出现了错误而在正常控制流以外采取的行为。
捕获异常
(1)监控区域(guarded region):一段可能产生异常的代码,并且后面要跟随处理这些异常的代码;
(2)Java使用try{ }块放置异常可能抛出异常的代码,并使用catch块捕获try块所有抛出的异常,使用finally块进行清除;

try{
    
       检查语句序列;}
catch(Exception ex){
    
        异常发生的处理语句 }
finally{
    
        一定要执行的语句}

常见异常

RuntimeException
错误的类型转换数组下标越界空指针访问
IOExeption
从一个不存在的文件中读取数据越过文件结尾继续读取EOFException连接一个不存在的URL

举例

public class Test01{
    
    
    public static void main(String[] args){
    
    
        String friends[] = {
    
    "lisa","bily","kessy"};
        try{
    
    
            for(int i=0;i<5;i++) {
    
    
                System.out.println(friends[i]);
            }
        }catch(ArrayIndexOutOfBoundsException e){
    
    
            System.out.println("index err");
        }
        System.out.println("\nthis is the end");
    }
}
程序Test01运行结果:lisa    bily    kessy
        index err this is the end   

public class DivideZero1{
    
    
    int x;
    public static void main(String[] args) {
    
    
        int y;
        DivideZero1 c=new DivideZero1();
        try{
    
    
            y=3/c.x;
        }
        catch(ArithmeticException e){
    
    
            System.out.println("divide by zero error!");
        }
        System.out.println("program ends ok!");
    }
}
程序DivideZero1运行结果:
        divide by zero error!
        program ends ok!


声明抛出异常

声明抛出异常是Java中处理异常的第二种方式
如果一个方法(中的语句执行时)可能生成某种异常,但是并不能确定如何处理这种异常,则此方法应显式地声明抛出异常,表明该方法将不对这些异常进行处理,而由该方法的调用者负责处理。
在方法声明中用 throws 子句可以声明抛出异常的列表,throws后面的异常类型可以是方法中产生的异常类型,也可以是它的父类。
声明抛出异常举例:

public void readFile(String file)  throws FileNotFoundException {
    
    
        ……
        // 读文件的操作可能产生FileNotFoundException类型的异常
        FileInputStream fis = new FileInputStream(file);
        ..……
        }

人工抛出异常

Java异常类对象除在程序执行过程中出现异常时由系统自动生成并抛出,也可根据需要人工创建并抛出
首先要生成异常类对象,然后通过throw语句实现抛出操作(提交给Java运行环境)。
IOException e =new IOException();
throw e;
可以抛出的异常必须是Throwable或其子类的实例。下面的语句在编译时将会产生语法错误:

public class Test01{
    
    
    public void regist(int num) throws MyException {
    
    
        if (num < 0)
            throw new MyException(“人数为负值,不合理”,3);
        else
            System.out.println("登记人数" + num );
    }
    public void manager() {
    
    
        try {
    
    
            regist(100);
        } catch (MyException e) {
    
    
            System.out.print("登记失败,出错种类"+e.getId());
        }
        System.out.print("本次登记操作结束");
    }
    public static void main(String args[]){
    
    
        Test8_6 t = new Test8_6();
        t.manager();

附件
springBuffer用例

package tyichang;

public class E18 {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("------------------------");
        add();
        System.out.println("------------------------");
        update();
        System.out.println("------------------------");
        delete();
    }

    public static void add() {
    
    
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("ABC");
        System.out.println(stringBuffer);
        stringBuffer.append("HER");
        System.out.println(stringBuffer);
        stringBuffer.insert(1, "de");
        System.out.println(stringBuffer);

    }

    public static void update() {
    
    
        StringBuffer stringBuffer = new StringBuffer("AAAAAAA");
        stringBuffer.setCharAt(2, 'C');
        System.out.println(stringBuffer);
        stringBuffer.replace(3, 5, "de");
        System.out.println(stringBuffer);
        System.out.println(stringBuffer.reverse());
    }

    public static void delete(){
    
    
        StringBuffer stringBuffer = new StringBuffer("AAVAAAAA");
        stringBuffer.delete(3,5);
        System.out.println(stringBuffer);
        stringBuffer.deleteCharAt(2);
        System.out.println(stringBuffer);
        stringBuffer.delete(0,stringBuffer.length());
        System.out.println(stringBuffer);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_44909275/article/details/105481330