异常和错误

  1. 程序错误
    1. 错误是必须要修改代码才可以修复的
    2. 必须要将相关的代码进行修改,避免错误的出现
  2. 异常

    1. 并不是由于代码的问题导致的,可以不修改代码进行修复
    2. 可以允许异常的出现,但是要根据情况捕获异常进行相关处理

      基本使用

  3. 定义一份可能出现异常的代码

    1. int money = scanner.nextInt();
    2. money = money /10;
    3. System.out.println(money);
  4. 使用try…catch捕获异常,并且进行相关的处理

    1. Scanner scanner = new Scanner(System.in);
    2. try{
    3. int money = scanner.nextInt();
    4. money = money /10;
    5. System.out.println(money);
    6. }catch(Exception e){
    7. System.out.println("请输入正确的数字");
    8. //自带的打印异常信息的方法
    9. // e.printStackTrace();
    10. //System.out.println(e.getMessage());
    11. }

    编程中常见异常

  5. Exception是所有的异常的父类,在其基础上根据异常不同,其子类继承之后拥有不同的类型

  6. 常见异常

    1. NullPointerException:空指针异常

      1. try {
      2. Student s = null;
      3. s.setName("小明");
      4. }catch (NullPointerException e){
      5. System.out.println("输入的用户不可以为空");
      6. }
    2. ArrayIndexOutOfBoundsException:数组越界异常

      1. Integer[] arrs = new Integer[10];
      2. int index = 85;
      3. try{
      4. arrs[index] = 0;
      5. }catch (ArrayIndexOutOfBoundsException e){
      6. System.out.println("数组越界异常");
      7. }
    3. ClassCastException: 类型转换异常

      1. Object s = new Student("小明",15);
      2. try {
      3. Person p = (Person) s;
      4. }catch (ClassCastException e){
      5. System.out.println("学生不可以转换成person类");
      6. }
    4. 其他异常

      1. //4NumberFormatException格式异常
      2. try {
      3. int n=Integer.parseInt("100a");
      4. System.out.println(n);
      5. }catch (NumberFormatException e){
      6. e.printStackTrace();
      7. }
      8. //5ArithmeticExceptioin算术异常
      9. int m=10/0;
      10. System.out.println(m);

      异常的创建与抛出

  7. 可以手动抛出异常(但是抛出异常必须要被处理)

    1. String input = scanner.next();
    2. if(input.equals("脏话")){
    3. //抛出异常,必须要被处理
    4. throw new Exception();
    5. }
  8. 抛出的异常有两种处理方式

    1. 方式1:直接进行处理(毫无意义)

      1. try {
      2. throw new Exception();
      3. } catch (Exception e){
      4. System.out.println("不许说脏话");
      5. }
    2. 方式2:将异常通过方法抛出去,在调用这个方法的时候处理异常

      1. //获取输入值
      2. public static String getInput() throws Exception {
      3. Scanner scanner = new Scanner(System.in);
      4. String input = scanner.next();
      5. if(input.equals("脏话")) {
      6. //抛出异常,必须要被处理
      7. throw new Exception();//相当于return
      8. //程序已经结束来了
      9. }
      10. return input;
      11. }
      12. public static void main(String[] args) {
      13. System.out.println("请输入聊天记录");
      14. try {
      15. String result = getInput();
      16. }catch (Exception e){
      17. System.out.println("不准输入脏话");
      18. }
      19. }

      自定义不同类型的异常

  9. 创建一个类继承异常

  10. 然后在其他代码中直接new出一个自定义的异常
  11. 登录案例

    1. 创建没有用户名的异常

      1. public class UsernameNotExistException extends Exception{
      2. }
    2. 创建密码不存在异常

      1. public class PasswordFailException extends Exception {
      2. }
    3. 在代码中抛出异常,并且分开进行处理

      1. public static void main(String[] args) {
      2. Scanner scanner = new Scanner(System.in);
      3. String username;
      4. String password;
      5. System.out.println("请输入用户名");
      6. username = scanner.next();
      7. System.out.println("请输入密码");
      8. password = scanner.next();
      9. try {
      10. login(username,password);
      11. //如果login抛出了异常try中之后的代码是不会再允许的
      12. //如果没有异常那么登录成功
      13. System.out.println("登录成功");
      14. }catch (UsernameNotExistException e){//用户名称错误异常
      15. System.out.println("用户不存在");
      16. }catch(PasswordFailException e){//密码错误异常
      17. System.out.println("密码错误");
      18. }
      19. }
      20. //登录方法
      21. public static void login(String username,String pwd)
      22. throws UsernameNotExistException, PasswordFailException {
      23. if(!username.equals("张三")){
      24. throw new UsernameNotExistException();
      25. }
      26. if(!pwd.equals("123")){
      27. throw new PasswordFailException();
      28. }
      29. }
  12. 重写异常的相关方法,提高异常的功能

    1. 创建一个类继承exception,添加消息数据,重写获取消息方法

      1. public class LoginFailException extends Exception{
      2. String msg;//用于存储异常的说明
      3. //重载构造方法
      4. public LoginFailException(String msg) {
      5. this.msg = msg;
      6. }
      7. //重写getMessage方法
      8. @Override
      9. public String getMessage() {
      10. return this.msg;
      11. }
      12. }
    2. 调用异常

      1. //实现登录方法
      2. public static void login(String username,String pwd)
      3. throws LoginFailException {
      4. //判断账号是否正确
      5. if(!username.equals("张三")){
      6. //抛出账号错误异常
      7. throw new LoginFailException("用户不存在");
      8. }
      9. //判断密码是否正确
      10. if(!pwd.equals("123")){
      11. //抛出密码错误的异常
      12. throw new LoginFailException("密码错误");
      13. }
      14. }
      15. public static void main(String[] args) {
      16. //输出信息的代码
      17. Scanner scanner = new Scanner(System.in);
      18. String username;
      19. String password;
      20. while(true){
      21. System.out.println("请输入用户名");
      22. username = scanner.next();
      23. System.out.println("请输入密码");
      24. password = scanner.next();
      25. //登录代码
      26. try {
      27. login(username,password);
      28. System.out.println("登录成功");
      29. break;
      30. } catch (LoginFailException e) {
      31. System.out.println(e.getMessage());
      32. }
      33. }
      34. System.out.println("程序结束");
      35. }

      检查异常和非检查异常

  13. 检查异常:异常必须要被处理

    1. 继承了是Exception
    2. 一些程序比如文件操作,网络操作,线程等等,可能是由于硬件的缘故爆出的异常都应该被强制检查
  14. 非检查异常:可以不用强制处理
    1. 继承的是RuntimeException
    2. 有一些程序,出现的异常是由于用户输入的错误,计算的错误等等,可以通过相关代码进行规避的异常,可以不被强制执行
  15. 一般情况下,自定义的异常都是选择继承RuntimeException

    异常处理的关键字

  16. try: 包裹可能会出现异常的代码,

    1. 如果在try中某一行代码抛出了异常,会直接进入到对应异常的catch代码中,try中的程序都中止了。
    2. 但是try外部的程序还是可以继续运行
  17. catch:是处理异常的代码

    1. 会将抛出的异常传递到catch的代码快中
    2. catch可以接收多个异常处理的
    3. 如果catch是多个异常的父类,那么在抛出多个不同异常的时候,会直接进入到这个catch父类的代码中
      1. try{
      2. login(username,password);
      3. //如果login抛出了异常try中之后的代码是不会再允许的
      4. //如果没有异常那么登录成功
      5. System.out.println("登录成功");
      6. }catch (Exception e){
      7. if( e instanceof UsernameNotExistException){
      8. System.out.println("用户不存在");
      9. }else if(e instanceof PasswordFailException){
      10. System.out.println("密码不正确");
      11. }
      12. }
  18. finally: 其中的代码无论是否出现异常都被执行

    1. try…catch…finally的使用

      1. try {
      2. login(username,password);
      3. System.out.println("登录成功");
      4. break;
      5. } catch (LoginFailException e) {
      6. System.out.println(e.getMessage());
      7. } finally {
      8. System.out.println("本次输入结束");
      9. }
    2. try…finally

      1. try{
      2. System.out.println("执行的相关");
      3. int i=1/0;
      4. }finally {
      5. System.out.println("程序中止了");
      6. }