一、什么是异常
public class ExceptionTest1 {
public static void main(String[] args) throws ParseException{
//Integer.valueOf("abc");
// int[] arr = {1,2,3};
// System.out.println(arr[5]);
/* try {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d=sdf.parse("2028-11-11 10:24:54");
System.out.println(d);
} catch (ParseException e) {
e.printStackTrace();
}*/
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d=sdf.parse("2028-11-11 10:24");
System.out.println(d);
}
}
二、异常的体系
三、抛出异常
四、自定义异常
AgeIllegalException
//必须让这个类继承自Exception,才能成为一个编译时异常类。
public class AgeIllegalException extends Exception{
public AgeIllegalException() {
}
public AgeIllegalException(String message) {
super(message);
}
}
AgeIllegalRuntimeException
//必须让这个类继承自RuntimeException,才能成为一个运行时异常类。
public class AgeIllegalRuntimeException extends RuntimeException{
public AgeIllegalRuntimeException() {
}
public AgeIllegalRuntimeException(String message) {
super(message);
}
}
public class ExceptionTest2 {
public static void main(String[] args) {
// 需求:保存一个合法的年龄
try {
saveAge(130);
System.out.println("底层执行成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("底层出现了bug");
}
try {
saveAge2(100);
System.out.println("saveAge2底层执行成功");
} catch (AgeIllegalException e) {
e.printStackTrace();
System.out.println("saveAge2底层出现了bug");
}
}
//运行时异常
public static void saveAge(int age) {
if (age > 0 && age < 120) {
System.out.println("年龄被成功保存:" + age);
} else {
// 用一个异常对象封装这个问题
throw new AgeIllegalRuntimeException("/age is illegal,your age is " + age);
}
}
//编译时异常 throws用在方法上,抛出方法内部的异常
public static void saveAge2(int age) throws AgeIllegalException {
if (age > 0 && age < 120) {
System.out.println("年龄被成功保存:" + age);
} else {
// 用一个异常对象封装这个问题
throw new AgeIllegalException("/age is illegal,your age is " + age);
}
}
}
五、异常的作用
public class ExceptionTest3 {
public static void main(String[] args) {
try {
test1();
} catch (ParseException e) {
System.out.println("解析的时间有问题");
e.printStackTrace();//打印出这个异常信息记录下来
} catch (FileNotFoundException e) {
System.out.println("要找的文件不存在");
e.printStackTrace();
}
}
public static void test1() throws ParseException, FileNotFoundException {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d=sdf.parse("2028-11-11 10:24");
System.out.println(d);
test2();
}
public static void test2() throws FileNotFoundException {
//读取文件
InputStream is=new FileInputStream("d:/test.txt");
}
}
public class ExceptionTest3_2 {
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 d=sdf.parse("2028-11-11 10:24");
System.out.println(d);
test2();
}
public static void test2() throws Exception {
//读取文件
InputStream is=new FileInputStream("d:/test.txt");
}
}
六、异常常见处理方式
/**
* 掌握异常的处理方式:捕获异常,尝试修复
*/
public class ExceptionTest4 {
public static void main(String[] args) {
//需求:调用一个方法,让用户输入一个合适的价格返回为止
//思路:使用循环,捕获异常,提示用户重新输入尝试修复
while (true) {
try {
System.out.println("合适的价格是:"+getPrice());
break;
} catch (Exception e) {
System.out.println("请输入合法的数字!!!!");
}
}
}
public static double getPrice(){
Scanner sc=new Scanner(System.in);
while (true) {
System.out.println("请输入合适的价格");
double price=sc.nextDouble();
if(price>=0){
return price;
}else{
System.out.println("您输入的价格是不合适的");
}
}
}
}