7.28总结

1.static关键字:

1.1static代表的是静态的,和对象无关,在对象创建之前就存在。static可以修饰成员变量,成员方法,代码块。
1.1.1 static 修饰成员变量直接用类调用
class Monkey{
    static String name;
    static  int age;
    
}
public class Demo1 {
    public static void main(String[] args) {
        Monkey.name = "小猴子";
        Monkey.age = 5;
        System.out.println(Monkey.name);
        System.out.println(Monkey.age);
    }
}
1.1.2 static 修饰成员方法
class Monkey{
    static String name;
    static  int age;
    //static 修饰的方法,直接用类调用
    public static  void eat(){
        System.out.println("吃香蕉");
    }
    //不用static修饰的成员方法,用对象调用
    public void run(){
        System.out.println("四脚着地跑");
    }

}
public class Demo1 {
    public static void main(String[] args) {
        Monkey.name = "小猴子";
        Monkey.age = 5;
        System.out.println(Monkey.name);
        System.out.println(Monkey.age);
        Monkey.eat();
        Monkey monkey = new Monkey();
        monkey.run();
    }
}

1.1.3 static修饰代码块
class Monkey{
    static String name;
    static  int age;
    //static 修饰的方法,直接用类调用
    public static  void eat(){
        System.out.println("吃香蕉");
    }
    //不用static修饰的成员方法,用对象调用
    public void run(){
        System.out.println("四脚着地跑");
    }
    //静态代码块
    static {
        System.out.println("一只猴子");
    }
    //构造代码块
    {
        System.out.println("真的是一只猴子");
    }

}
public class Demo1 {
    public static void main(String[] args) {
        Monkey.name = "小猴子";
        Monkey.age = 5;
        System.out.println(Monkey.name);
        System.out.println(Monkey.age);
        Monkey.eat();
        Monkey monkey = new Monkey();
        monkey.run();
    }
}

在程序中,静态代码块是最先执行的,然后才执行构造代码块。

2.异常

2.1 异常的分类:编译时异常和运行时异常

2.1.1 编译时异常。

OException:输入输出流异常

FileNotFoundException:文件找不到的异常

ClassNotFoundException:类找不到的异常

DataFormatException:数据格式化异常

NoSuchFieldException:没有匹配的属性异常

NoSuchMethodException:没有匹配的方法异常

SQLException:数据库操作异常

TimeoutException:执行超时异常

编译时异常时在编译时就会出错,程序不可以被运行。

2.1.2 运行时异常。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HVQLwH1T-1659012803845)(C:\Users\28013\AppData\Roaming\Typora\typora-user-images\image-20220728203947529.png)]

运行时异常是在程序允许时出现异常,当有行代码出现异常时,下面的代码将不会再执行。

2.2异常的捕捉和抛出
2.2.1异常的捕捉

语法格式:

try{
    
} catch (Exception e) {
    e.printStackTrace();
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ruPfRBVj-1659012803850)(C:\Users\28013\AppData\Roaming\Typora\typora-user-images\image-20220728204445817.png)]

将异常捕捉后,虽然异常还是存在,但是不会影响代码继续向下执行。

2.2.2 异常的抛出:

和异常捕捉不同的是,当异常抛出时一旦出现异常,则程序将会立即停止。

抛出的关键字为:throw 和throws

throw是抛出异常时的一个动作。

throws是告诉人们此处抛出了异常,请务必小心。

package com.qfedu.c_Exception;

import java.io.FileNotFoundException;

public class Demo8 {
	public static void main(String[] args) throws Exception{
		test(0);
		Thread.sleep(1000);
	}
	public static void test (int a) throws FileNotFoundException{
		if (a == 0) {
			//编译时异常
			throw new FileNotFoundException();
		}
		System.out.println("jvm xixi");
	}

}

id test (int a) throws FileNotFoundException{
if (a == 0) {
//编译时异常
throw new FileNotFoundException();
}
System.out.println(“jvm xixi”);
}

}




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值