接口完结+异常捕获、抛出、自定义小结

接口的作用

  1. 约束

  2. 定义一些方法,让不同的人去实现

  3. public abstract

  4. public static final

  5. 接口不能被实例化,接口中没有构造方法

  6. implements可以实现多个接口

  7. 必须要重写接口中的方法

public class Outer {
    //局部内部类
   public void method(){
       class Inner{
           public void in(){
​
           }
       }
   }
}
//一个Java类中可以有多个class类,但是只能由public class
class A{
​
}
/* private int id = 10;
    public void out(){
        System.out.println("这是外部类的方法!");
    }
    class Inner{
        public void in(){
            System.out.println("这是内部类的方法!!");
        }
        public void getID(){
            System.out.println(id);
        }
    }*/
public class day70 {
    public static void main(String[] args) {
        //new
        Outer outer = new Outer();
        //通过这个外部类来实例化内部类
         /*Outer.Inner inner = outer.new Inner();
        inner.in();
        inner.getID();*/
    }
}

匿名类

public class Test {
    public static void main(String[] args) {
        //没有名字去初始化类,不用讲实力保存到变量中
        new Apple().eat();
        //Apple apple = new Apple();
        UserServise userServise = new UserServise() {
​
            @Override
            public void hello() {
​
            }
        };
    }
}
class Apple{
    public void eat(){
        System.out.println("1");
    } 
}
interface UserServise{
    void hello();
}

异常

ERROR类对象通常是由Java虚拟机生成并抛出,大多数错误域代码编写者所执行的操作无关。

异常处理机制

抛出异常

捕获异常

5个关键字:try catch finally throw throws

public class bitter01 {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        try {//try 监控区域
            
            System.out.println(a/b);
        }catch(ArithmeticException e){//catch捕获异常
            System.out.println("程序出现异常!,变量b不能为零");
    }finally {//finally 处理善后工作
            System.out.println("finally");
        }
        //finally 可以不要但是try 和catch必须要有 假设IO,资源,关闭!
        }
       
        }
}
public class bitter01 {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        try {//try 监控区域
            System.out.println(a/b);
        }catch(Error e){//catch(想要捕获的异常类型)捕获异常
            System.out.println("程序出现异常!,变量b不能为零");
    }catch (Exception e){
            System.out.println("exception");
        }catch(Throwable e){
            System.out.println("Throwable");
        } finally {//finally 处理善后工作
            System.out.println("finally");
        }
​
        //finally 可以不要但是try 和catch必须要有 假设IO,资源,关闭!
        }
        public void a(){
        b();
        }
        public void b(){
        a();
        }
}
​

假设要捕获多个异常: 一定要从小到大;

public class bitter01 {
    public static void main(String[] args) {
        try {
            new bitter01().test(1,0);
        } catch (ArithmeticException e) {
            e.printStackTrace();
        } 
​
    }
        //见识过和这个方法中处理不了这个异常,。方法上抛出异常
        public void   test(int a,int b) throws ArithmeticException(){
        if(b==0){// throw throws
            throw new ArithmeticException();//主动抛出异常,一般在方法中使用。
        }
        System.out.println(a/b);
    }
​
}

自定义异常

public class bitter04 {
    //可能会存在异常的方法
    static void test (int a) throws bitter03 {
        System.out.println("传递的参数为:"+a);
        if(a>10){
            throw new bitter03(a);//抛出去
        }
        System.out.println("ok");
    }
​
    public static void main(String[] args) {
        try {
            test(11);
        } catch (bitter03 e) {
            System.out.println("bitter03-->"+e);
        }
    }
    }

总结

  1. 处理异常时,采用逻辑合理规避同时辅助try-catch处理

  2. 在多重catch块后面,可以加一个catch(Exception)来处理可能会被遗漏的异常

  3. 对于不确定的代码,也可以加上try-catch,处理存在的异常

  4. 尽量的去处理异常,切记只是简单的调用peintStackTrace()去打印输出

  5. 尽量添加finally语句块去释放占用的资源。IO~ Scanner

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值