异常处理
6. 抛出异常
package cn.zixinyuan;
import cn.webrx.SexException;
import lombok.Data;
/**
* <p>Project: zixinyuan - OracleUtil
*
* @author zixinyuanya [2991228540@qq.com]
* @version 1.0
* @since 17
*/
@Data
public class Student1 {
private int id;
private String name;
private String gender;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) throws SexException {
if("男".equals(gender) || "女".equals(gender)) {
this.gender = gender;
}else{
throw new SexException("性别设置异常,必须为男或女");
}
this.gender = gender;
}
}
测试
package cn.zixinyuan;
import cn.webrx.SexException;
/**
* <p>Project: zixinyuan - OracleUtil
*
* @author zixinyuanya [2991228540@qq.com]
* @version 1.0
* @since 17
*/
public class Test {
public static void main(String[] args) {
Student stu = new Student();
stu.setId(10); stu.setName("jack");
try {
stu.setGender("男2");
int a = 10 / 20;
} catch (SexException e) {
System.out.println("性别设置错误。");
} catch (ArithmeticException e) {
System.out.println("分母为0了");
} catch (Exception e) {
System.out.println("未知exception");
}
}
}
7 finally块执行
package cn.zixinyuan;
import cn.webrx.SexException;
import org.jsoup.Jsoup;
import java.io.IOException;
/**
* <p>Project: zixinyuan - OracleUtil
*
* @author zixinyuanya [2991228540@qq.com]
* @version 1.0
* @since 17
*/
public class Test1 {
public static void main(String[] args) {
Student stu = new Student();
stu.setId(10); stu.setName("jack");
try {
stu.setGender("男");
Jsoup.connect("https://mi.com").get();
} catch (SexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
show();
}
public static void show() {
try {
Jsoup.connect("http://www.baidu.com").get();
System.out.println("try...");
return;
} catch (IOException e) {
System.out.println("catch....");
return;
}finally {
System.out.println("finally....");
}
}
}
8. try-with-resources语句
package cn.zixinyuan;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* <p>Project: zixinyuan - OracleUtil
*
* @author zixinyuanya [2991228540@qq.com]
* @version 1.0
* @since 17
*/
public class Exception {
public static void main(String[] args) {
String f = "user.txt";
try (var fw = new FileWriter(f);
var fr = new BufferedReader(new FileReader(f))) {
fw.write("hello world 中文1\r\n");
fw.flush();
System.out.println(fr.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
9. finalize()方法
垃圾回收机器(Garbage Collection),也叫GC,垃圾回收器主要有一下特点:
- 当对象不再被程序所使用的时候,垃圾回收器将会将其回收
- 垃圾回收是在后台运行的,我们无法命令垃圾回收器马上回收资源,但是我们可以告诉他可以 尽快回收资源(System.gc()和Runtime.getRuntime().gc())
3.垃圾回收器在回收某个对象的时候,首先会调用该对象的finalize()方法
4.GC主要针对堆内存
finalize()
是Object里面的一个方法,当一个堆空间中的对象没有被栈空间变量指向的时候,这个对象会等待被java回收: jdk
里面是这样实现的:
protected void finalize() throws Throwable { }
}
案例测试代码:
public class testFinalize{
public static void main(String[] args){
Person p = new Person();
for(int i = 0 ; i< 1000; i++){
p = null;
}
System.gc();//增加垃圾回收器启动的概率
}
}
class Person{
protected void finalize() throws Throwable{
System.out.println("垃圾准备回收中!!!");
}
}