内部类与异常

内部类

  • 内部类就是在一个类的内部在定义一个类,比如,A类中定义了 一个B类,那么B类相对A类来说就称为内部类,而A类相对B类来说就是外部类了

  • 成员内部类

  • 静态内部类

  • 局部内部类

package opp.oop10;
​
public class App {
    public static void main(String[] args) {
​
        Outer outer = new Outer();
​
        //通过这个外部类来实例化这个内部类
        Outer.Inter inter = outer.new Inter();
        inter.in();
        inter.getID();
​
​
    }
}
package opp.oop10;
​
public class Outer {
​
    private int id=9;
    public void out(){
        System.out.println("外部类的方法");
    }
​
    public class  Inter{
        public void in(){
            System.out.println("内部类的方法");
        }
​
        //获得外部类的私有属性
        public void getID(){
            System.out.println(id);
        }
​
        //局部内部类
        public void method(){
            class Inner{
                public void in(){
​
                }
​
            }
        }
​
        }
    }
  • 匿名内部类

package opp.oop10;
​
public class Test {
    public static void main(String[] args) {
        //匿名内部类:不用将实例保存到变量中
        new Apple().eat();
​
        new UserService(){
            @Override
            public void hello() {
​
            }
        };
    }
​
}
class Apple{
    public void eat(){
        System.out.println("1");
    }
}
interface UserService{
    void hello();
}

异常

  • 要理解Java异常处理是如何工作的,你需要掌握以下三种类型的异常:

    • 检查性异常:最具代表的检查性异常时用户错误或问题引起的异常,这是程序员无法预见的。例如要打开一个不存在文件时,一个异常就发生了,这些异常在编译时不能被简单地忽略

    • 运行时异常:运行时异常是可能被程序员避免的异常。与检查性异常相反,运行时异常可以在编译时被忽略

    • 错误ERROR:错误不是异常,而是脱离程序员控制的问题。错误在代码中通常被忽略。例如:当栈溢出时,一个错误就发生了,它们在编译也检查不到的

处理机制

  • 抛出异常

  • 捕获异常

  • 异常处理五个关键字:

    • try、catch、finally、throw、throws

package com.mao.exception;
​
public class T1 {
    public static void main(String[] args) {
        int a=1;
        int b=0;
​
        try {
            System.out.println(a/b);
        } catch (Exception e) {
            throw new RuntimeException(e);//打印错误的栈信息
        } finally {
        }
    }
}
package com.mao.exception;
​
import java.util.SortedMap;
​
public class Test {
    public static void main(String[] args) {
        try {
            new Test().test(1,0);
        } catch (ArithmeticException e) {
            throw new RuntimeException(e);
        }
​
​
    }
    //假设在这个方法中处理不了这个异常,方法上抛出异常
    public void test(int a,int b) throws ArithmeticException{
        if (b==0){
            throw new ArithmeticException();//主动的抛出异常,一般在方法中使用
        }
        System.out.println(a/b);
    }
​
}
/*
try {//try监控区域
            if (b==0){
                throw new ArithmeticException();//主动的抛出异常
            }
            System.out.println(a/b);
        }catch (ArithmeticException e){//catch(想要捕获的异常类型!)捕获异常
            System.out.println("程序出现异常:变量b不能为0!");
        }finally {//finally处理善后工作
            System.out.println("finally");
        }
 */

自定义异常

  • 使用Java内置的异常类可以描述在编程时出现的大部分异常情况。除此之外,用户还可以自定义异常。用户自定义异常类,只需继承Exception类即可

  • 在程序中使用自定义异常类,大体可分为以下几个步骤:

    1. 创建自定义异常类

    2. 在方法中通过throw关键字抛出异常对象

    3. 如果在当前抛出异常的方法中处理异常,可以使用try-catch语句捕获并处理;否则在方法的声明处通过throws关键字指明要抛出给方法调用者的异常,继续进行下一步操作

    4. 在出现异常方法的调用者中捕获并处理异常

package com.mao.exception;
​
​
//自定义的异常
public class MyException extends Exception{
    //传递数字>10
    private int detail;
​
    public MyException(int a){
        this.detail=a;
    }
​
    //toString:异常的打印信息
    @Override
    public String toString() {
        return "MyException{" + detail + '}';
    }
}
package com.mao.exception;
​
public class T2 {
​
    //可能会存在异常的方法
    static void test(int a) throws MyException{
        System.out.println("传递的参数为:"+a);
        if(a>10){
            throw new MyException(a);//抛出
        }
        System.out.println("OK");
​
    }
​
    public static void main(String[] args) {
        try {
            test(11);
        } catch (MyException e) {
            System.out.println("MyException=>"+e);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值