Exception:叫异常,它代表的才是我们程序可能出现的问题,所以,我们通常会用Exception以及它的孩子来封装出现出现的问题。
自定义运行时异常
- 定义一个异常类继承RuntimeException
- 重新构造器
- 通过throw new 异常类(xxx)来创建异常对象并抛出
? ? ? ? 编译阶段不报错,提醒不强烈,运行时才可能出现
Test类
public class Test {
public static void main(String[] args) {
try {
saveAge(233);
System.out.println("底层执行成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("底层出现了bug");
}
}
public static void saveAge(int age){
if (age > 0 && age <150){
System.out.println("年龄被保存成功:" + age);
}else {
throw new AgeIllegalRuntimeException("/age is illegal, you age is " + age);
}
}
}
?AgeIllegalRuntimeException类
public class AgeIllegalRuntimeException extends RuntimeException{
public AgeIllegalRuntimeException() {
}
public AgeIllegalRuntimeException(String message) {
super(message);
}
}
?
自定义编译时异常
- 定义一个异常类继承Exception
- 重写构造器
- 通过throw new 异常类(xxx)来创建异常对象并抛出
????????编译阶段就报错,提醒更加强烈
Test类
public class Test {
public static void main(String[] args) {
try {
saveAge(23);
System.out.println("底层执行成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("底层出现了bug");
}
}
public static void saveAge(int age) throw AgeIllegalException{
if (age > 0 && age <150){
System.out.println("年龄被保存成功:" + age);
}else {
throw new AgeIllegalException("/age is illegal, you age is " + age);
}
}
}
?AgeIllegalException类
public class AgeIllegalException extends Exception{
public AgeIllegalException() {
}
public AgeIllegalException(String message) {
super(message);
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
try {
test1();
} catch (FileNotFoundException e) {
System.out.println("您要找的文件不存在");
e.printStackTrace();
} catch (ParseException e) {
System.out.println("您要解析的时间有问题");
e.printStackTrace();
}
}
public static void test1() throws FileNotFoundException, ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2021-10-10 10:24");
System.out.println(date);
test2();
}
private static void test2() throws FileNotFoundException {
// 读取文件的
InputStream is = new FileInputStream("D:/abcd.png");
}
}
优化上面的写法
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
try {
test1();
} catch (Exception e) {
System.out.println("您当前操作有问题");
e.printStackTrace();
}
}
public static void test1() throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2021-10-10 10:24");
System.out.println(date);
test2();
}
private static void test2() throws Exception {
// 读取文件的
InputStream is = new FileInputStream("D:/abcd.png");
}
}
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
while (true) {
try {
System.out.println(getMoney());
break;
} catch (Exception e) {
System.out.println("请您输入合法的数字");
}
}
}
public static double getMoney(){
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("请您输入合适的价格");
double money = sc.nextDouble();
if (money > 0){
return money;
}else {
System.out.println("您输入的价格是不合适的");
}
}
}
}