关于异常的部分处理

package exception;
/**

  • java异常处理机制中的try-catch

  • 语法:

  • try{

  • 程序代码片段

  • }catch(XXXException){

  • 程序中出现XXXException的处理手段代码

  • @author ChenYi

  • @date 2019.08.29
    /
    public class TryCatchDemo {
    public static void main(String[] args) {
    System.out.println(“程序开始”);
    try {
    // String str=null;
    // String str="";
    String str=“a”;
    /

    * 当JVM执行代码的过程中出现了某种异常后,会自动实例化该异常的一个实例,
    * 并将代码执行过程设置进去然后将其抛出
    */
    System.out.println(str.length());

     System.out.println(str.charAt(0));
     
     System.out.println(Integer.parseInt(str));
     /*
      * try语句块中报错代码以下的内容都不会执行
      */
     System.out.println("???????????????????");
     }catch (NullPointerException e ) {
     	System.out.println("空指针异常");
     	/*
     	 * catch可以定义多个,针对try中不同的异常处理手段时,我们应当分别捕获他们
     	 */
     }catch (StringIndexOutOfBoundsException e) {
     	System.out.println("数组下标越界异常");
     	/*
     	 * 在最后一个catch处捕获Exception可以避免因为一个未处理的异常导致程序中断.
     	 */
     }catch (Exception e) {
     	System.out.println("管你什么异常,反正就是有错!");
     }
     
     System.out.println("程序结束");
    

    }
    }

package exception;
/**

  • finally是异常处理机制的最后一块
  • 他可以直接跟在try之后,或者最后一个catch之后.
  • fanally可以保证只要代码执行到try当中,
  • 无论try中的代码是否出现异常finally都必须执行.
  • @author ChenYi
  • @date 2019.08.29

/
public class FinallyDemo {
public static void main(String[] args) {
System.out.println(“程序开始了”);
try {
String str=null;
System.out.println(str.length());
/

* return后也是先执行finally后才返回的
*/
return;
} catch (Exception e) {
System.out.println(“出错了!”);
}finally {
System.out.println(“finally中的代码执行了!”);
}
System.out.println(“程序结束了”);
}
}

package exception;

import java.io.FileOutputStream;
import java.io.IOException;

import io.FOSDemo;

/**

  • 异常处理机制在IO当中的应用

  • @author ChenYi

  • @date 2019.08.29
    */
    public class FinallyDemo2 {
    public static void main(String[] args) {
    FileOutputStream fos = null;
    try {
    fos=new FileOutputStream(“fos.dat”);
    fos.write(1);
    fos.close();
    } catch (Exception e) {
    // TODO: handle exception
    System.out.println(“出错了!”);
    }finally {
    try {
    if (fos!=null) {

     			fos.close();
     		}
     	} catch (IOException e) {
     		// TODO Auto-generated catch block
     		e.printStackTrace();
     	}
    
     	System.out.println("程序结束");
     }
    

    }

}

package exception;

import java.io.FileOutputStream;

/**

  • JDK7之后推出了一个新的特性:autocloseable
  • 可以用更简短的代码完成异常处理中IO操作中的关闭
  • @author ChenYi
  • @date 2019.08.30
    /
    public class FinallyDemo3 {
    public static void main(String[] args) {
    System.out.println(“程序开始”);
    /

    * 注意,只有实现了AutoCloseable接口的类才可以在这里定义,并最终会被编译器改为在finally中close。
    * Java io 中的类都实现了这个接口
    */ //↓
    try (FileOutputStream fos=new FileOutputStream(“fos.txt”)){
    fos.write(1);
    } catch (Exception e) {
    System.out.println(“出错了”);
    }
    System.out.println(“程序完毕”);
    }
    }

package exception;
/**

  • finally常见面试题

  • 请分别说明:final,finally,finalize

  • finalize是一个方法,Object定义的方法,该方法是GC调用的,

  • 当GC即将释放一个对象时会调用该方法,该方法调用完毕后该对象会被释放

  • @author ChenYi

  • @date 2019.08.30
    */
    public class FinallyDemo4 {
    public static void main(String[] args) {
    System.out.println(test(“0”)+","+test(null)+","+test(""));

    }
    public static int test(String str) {
    try {
    return str.charAt(0)-‘0’;
    } catch (NullPointerException e) {
    return 1;
    } catch (Exception e) {
    return 2;
    } finally {
    // return 3;//必定运行,会覆盖之前的赋值
    }
    }
    }

package exception;
/**

  • 异常常见的方法

  • @author ChenYi

  • @date 2019.08.30
    */
    public class ExceptionApiDemo {
    public static void main(String[] args) {
    System.out.println(“程序开始”);

     try {
     	String line ="a";
     	System.out.println(Integer.parseInt(line));
     } catch (Exception e) {
     	//输出错误堆栈信息
     	e.printStackTrace();
     	//获取错误信息
     	String message = e.getMessage();
     	System.out.println(message);
     	
     	System.out.println("出错了");
     }
     System.out.println("程序结束");
    

    }
    }

package exception;
/**

  • 自定义异常

  • 自定义异常通常说明业务逻辑错误所表示的异常

  • 比如:年龄不合法异常.

  • @author ChenYi

  • @date 2019.08.30
    */
    public class IllegalAgeException extends Exception{

    /**

    */
    private static final long serialVersionUID = 1L;

    public IllegalAgeException() {
    super();
    // TODO Auto-generated constructor stub
    }

    public IllegalAgeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
    super(message, cause, enableSuppression, writableStackTrace);
    // TODO Auto-generated constructor stub
    }

    public IllegalAgeException(String message, Throwable cause) {
    super(message, cause);
    // TODO Auto-generated constructor stub
    }

    public IllegalAgeException(String message) {
    super(message);
    // TODO Auto-generated constructor stub
    }

    public IllegalAgeException(Throwable cause) {
    super(cause);
    // TODO Auto-generated constructor stub
    }

}

package exception;

/**

  • 使用当前类测试异常的抛出

  • @author ChenYi

  • @date 2019.08.30
    */
    public class Person {
    private int age;

    public int getAge() {
    return age;
    }

    public void setAge(int age) throws IllegalAgeException {
    if (age<0||age>100) {
    //抛出异常
    /*
    * 抛出异常
    * 当我们使用throw抛出异常时,除了RuntimeException之外的
    * 其他异常都必须在方法上使用throws声明该异常的抛出否则编译不通过
    */
    throw new IllegalAgeException(“年龄不合法”);
    }
    this.age = age;
    }

}

package exception;
/**

  • 异常的抛出
  • 通常遇到以下情况,我们会使用thorw主动抛出异常
  • 1:满足语法要求,但是不满足业务逻辑要求时
  • 2:确实出现了异常,但是该异常不应当在当前代码片段中被处理时可以对外抛出
  • @author ChenYi
  • @date 2019.08.30
    /
    public class ThrowDemo {
    public static void main(String[] args) {
    System.out.println(“程序开始了”);
    Person p=new Person();
    try {
    /

    * 当调用一个含有throws声明异常抛出的方法时,编译器会要求我们必须添加异常处理的手段,方式有两种:
    * 1.使用try-catch捕获并处理该异常
    * 2.在当前方法上继续使用throws声明异常抛出
    */
    //语法满足,但不符合业务
    p.setAge(1000);
    } catch (IllegalAgeException e) {
    System.out.println(“出错。。。。。。。。。。”);
    e.printStackTrace();
    }
    System.out.println(p.getAge());
    System.out.println(“程序结束了”);
    }
    }

package exception;

import java.awt.AWTException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;

/**

  • 子类重写超类含有throws声明异常抛出的方法时对throws的重写规则

  • @author ChenYi

  • @date 2019.08.30
    */
    public class ThrowsDemo {
    public void dosome() throws IOException,AWTException{}

    class Sub extends ThrowsDemo{
    // public void dosome() throws IOException,AWTException{}

     //允许不再抛出任何异常
    

// public void dosome() {}

	//允许抛出部分异常

// public void dosome() throws IOException{}

	//允许抛出超类方法抛出异常的子类型异常

// public void dosome() throws FileNotFoundException{}

	//不允许抛出额外异常

// public void dosome() throws SQLException{}

	//不允许抛出超类方法抛出异常的超类异常

// public void dosome() throws Exception{}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值