zoukankan      html  css  js  c++  java
  • JAVA Class18

    学习内容:

    1.异常:

    异常是会打断程序正常运行的事件。

    继承关系:Throwable是超类,其继承类为Error,Exception。

    常见的Error有StackOverflowError、OutofMemery错误,Error会直接打断整个程序运行,没有方法处理。

    Exception:分为运行时异常、编译时异常(可查异常),其中运行时异常不强行要求处理异常,而编译时异常必须处理,要么try catch住,要么往外抛,否则编译无法通过。

    异常报错过程:JVM检测到异常,实例化一个含异常信息的异常类,向调用者抛出异常,如果调用者未作处理,则抛向main方法,如果main方法也未作处理,则抛给JVM,JVM打印错误信息。

    2.异常处理:

    (1)try catch,在方法体内使用

    public class Test {
        public static void main(String[] args){
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date d1 = sf.parse("2018-04-22");
            } catch (ParseException e) {//可查异常,必须处理,要么try catch住,要么往外抛
                System.out.println("日期格式错误!");//异常未被抛出,只是打印语句,不打断程序运行
            } catch (Exception ex) {//平级异常、子类异常在前,父类异常在后
                System.out.println(ex);
            }
            
        }
    
    }
    public class Test {
        public static void main(String[] args){
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                int[] a = {1,2};
                 System.out.println(a[3]);
            } 
            catch (ArrayIndexOutOfBoundsException e) {//运行时异常,可以不处理,这里选择处理,并抛出异常
                throw new ArrayIndexOutOfBoundsException("下标越界");
            }
            
        }
    }

    运行时异常可以不try catch,可以直接抛出

    public Test{
        public static double getArea(double r) {
            if(r<=0) {
                throw new RuntimeException("错误的半径!");
            }
            return Math.PI*r*r;
        }
    }

    (2)throws

    直接抛出错误:

    public class Test {
        public static void main(String[] args) throws ParseException {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            Date d1 = sf.parse("2018-04-22");
        }
    
    }

    (3)throw和throws的区别:

    1. throws 出现在方法声明上,而throw通常都出现在方法体内。
    2. throws 表示出现异常的一种可能性,并不一定会发生这些异常;throw则是抛出了异常,执行throw则一定抛出了某个异常对象。

    (4)多异常处理时的注意事项:

    平级异常、子类异常在前,父类异常在后。

    (5)继承类异常处理问题:

    public class Fu {
        public void method() throws NullPointerException{}
        public void me() {}
    }
    public class Zi extends Fu{
        /*public void method() {}*/
        //子类重写可以不抛异常
        /*public void method() throws NullPointerException {}*/
        //如果要抛异常,则抛出的异常要与父类相同或者是父类抛出的的异常的子类
        //public void me() throws Exception{}
        //如果父类的方法没有抛异常,则子类也不可以抛出异常,如果子类重写的方法需要处理异常,
        //则只能 try catch
    }

     (6)try catch finally

    public Test{
       public static void t(int[] a) {
            try {
                System.out.println(a[3]);
            } catch (Exception e) {
                System.out.println(e);
            }finally {//无论是否发生异常都会执行
                a = null; //通常用来释放资源
            }
        }
    }

    3.自定义异常

    可以指定抛出的异常语句

    public class FException extends RuntimeException{
        public FException() {};
        public FException(String message) {
            super(message);
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            System.out.println(test(-2.5));
        }
    
    }

    4.Throwable常用方法:

    getMessage:返回异常信息
    toString:放回异常类名和异常信息
    printStackTrace:在控制台打印异常类名、异常信息、出现位置

  • 相关阅读:
    [LeetCode] Trips and Users 旅行和用户
    [LeetCode] Rising Temperature 上升温度
    [LeetCode] Delete Duplicate Emails 删除重复邮箱
    [LeetCode] Department Top Three Salaries 系里前三高薪水
    Spring boot Jackson基本演绎法&devtools热部署
    使用spring tool suite(STS)工具创建spring boot项目和出现错误后的处理
    Spring Boot 2.0官方文档之 Actuator
    springboot 使用webflux响应式开发教程(二)
    SpringBoot在自定义类中调用service层等Spring其他层
    springBoot单元测试-模拟MVC测试
  • 原文地址:https://www.cnblogs.com/whwjava/p/8881900.html
Copyright © 2011-2022 走看看