认识异常
1.运行时异常
下面这行代码是将字符串转化为数字,而输入abc会报错
Integer.valueOf("abc");
数组越界异常
int [] arr ={11,22,33};
System.out.println(arr[5]);
2.编译时异常
当你打出下面这行代码时,系统会爆红
提醒你要检查日期格式
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date d= sdf.parse("2028-11-11 10:28");
解决方法
1.利用Ctrl+Alt+T快捷键实现try+catch
2.throw ParseException
自定义异常
1.自定义运行时异常
//1.必须让这个类继承RuntimeException才能成为一个运行异常类
public class Test7_2AgelllegalRuntimeException extends RuntimeException{
public Test7_2AgelllegalRuntimeException() {
}
public Test7_2AgelllegalRuntimeException(String message) {
super(message);
}
}
重写构造器时选择前两个
else {
//用一个异常对象封装这个问题
//throw抛出这个异常问题
throw new Test7_2AgelllegalRuntimeException("/age is illegal age is "+age);
}
完整代码
//自定义异常
public class Test7_1exception {
public static void main(String[] args) {
//保存一个合法年龄
try {
saveAge(160);
System.out.println("底层执行成功");
} catch (Exception e) {
e.printStackTrace();//打印异常
System.out.println("底层出现问题");
}
}
public static void saveAge(int age)
{
if(age>0&&age<150)
{
System.out.println("年龄被成功保存"+age);
}
else {
//用一个异常对象封装这个问题
//throw抛出这个异常问题
throw new Test7_2AgelllegalRuntimeException("/age is illegal age is "+age);
}
}
}
当你输入223时会出现异常
2.自定义编译时异常
1,必须让这个类继承自Expection,才能成为一个异常类
//1,必须让这个类继承自Expection,才能成为一个异常类
public class Test7_3AgelllegalException extends Exception{
public Test7_3AgelllegalException() {
}
public Test7_3AgelllegalException(String message) {
super(message);
}
}
写出来就会爆红,提醒
解决方法
2.在方法外写上throws+异常名
注意:
throw 是抛出这个异常对象
throws是用在方法上,抛出方法内部异常
3.再在主函数调用此方法时再利用try+catch
try {
saveAge2(25);
} catch (Test7_3AgelllegalException e) {
e.printStackTrace();
}
同样的在输入225时会报错
异常的处理
1.捕获异常,记录异常并响应合适的信息给用户
先将test的问题抛出
public static void test2() throws FileNotFoundException {
//读取文件
InputStream is =new FileInputStream("D//meinv.nnn");
}
其中test1中调用了test2()再将两者的信息抛出
public static void test1() throws FileNotFoundException, ParseException {
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date d= sdf.parse("2028-11-11 10:28");
System.out.println(d);
test2();
}
最后再利用try+catch将问题抛出
public class Test7_4expection {
public static void main(String[] args) {
try {
test1();
} catch (FileNotFoundException e) {
System.out.println("您要找的文件不存在");
e.printStackTrace();//打印异常信息,记录异常信息
} catch (ParseException e) {
System.out.println("您解析的时间有问题");
e.printStackTrace();//打印异常信息,记录下来
}
}
改进版:将所有的问题用Expection抛出
public static void main(String[] args) {
try {
test1();
} catch (Exception e) {
System.out.println("您当前的操作有问题");
e.printStackTrace();//打印异常信息,记录异常信息
}
}
public static void test1() throws Exception,Exception {
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date d= sdf.parse("2028-11-11 10:28");
System.out.println(d);
test2();
}
public static void test2() throws Exception {
//读取文件
InputStream is =new FileInputStream("D//meinv.nnn");
}
}
2.捕获异常,尝试重新修复
用户乱输入导致异常
package com.itcollection;
import java.util.Scanner;
public class Test5_5expection {
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) {
System.out.println("请输入价格");
Scanner sc =new Scanner(System.in);
Double d = sc.nextDouble();
if(d>=0)
{
System.out.println("您输入的价格合适");
return d;
}
else {
System.out.println("您输入的价格不对");
}
}
}
}