java-异常
·
java异常体系
- Error代表系统级错误,开发人员不需要关注
- Exception代表程序可能出现的问题
1、运行时异常:RuntimeException,编译阶段不出现错误,运行时出现异常
public static void show() {
int[] arr = {10,20,30};
//System.out.println(arr[3]);
//System.out.println(10/0);
//空指针异常
String str = null;
System.out.println(str);
//System.out.println(str.length());
}
2、编译时异常
public static void show1(){
String str = "2025-09-08 12-13-15";
//把字符串转换成日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date date = sdf.parse( str);
System.out.println(date);
}
异常的基本处理
- 抛出异常(throws)
将方法内部的异常抛给调用者处理
public static void show1() throws ParseException {
String str = "2025-09-08 12-13-15";
//把字符串转换成日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date date = sdf.parse( str);
System.out.println(date);
}
- 捕获异常(try…catch)
public static void main(String[] args){
//show();
try {
//监视代码,出现异常会被catch拦截
show1();
} catch (ParseException e) {
e.printStackTrace();//输出异常信息
}
}
异常的作用
1、定位程序bug
2、作为方法内部一种特殊返回值,通知上层调用者方法的执行问题
public class demo2 {
public static void main(String[] args) {
System.out.println("程序开始执行...");
try{
int a=10;
int b=0;
int c=div(a,b);
System.out.println("结果为:"+c);
}catch(Exception e){
e.printStackTrace();
System.out.println("出现异常:"+e.getMessage());
}
}
//求两个数相除的结果
public static int div(int a,int b) throws Exception{
if(b==0){
System.out.println("除数不能为0");
//可返回一个异常给上层调用者
throw new Exception("除数不能为0");
}
int result=a/b;
return result;
}
}
自定义异常
- java无法为全部问题都提供异常类,若自己的问题想用异常表示,则需要自己定义异常类

public class AgeIllegalRuntimeException extends RuntimeException{
public AgeIllegalRuntimeException()
{
super();
}
public AgeIllegalRuntimeException(String message)
{
super(message);
}
}
public class demo4 {
public static void main(String[] args) {
//认识自定义异常--运行时异常
saveage(300);
}
//只要收到年龄小于1岁或大于200岁,就抛出一个异常
public static void saveage(int age){
if(age<1||age>200){//年龄不合法
throw new AgeIllegalRuntimeException("年龄不合法,年龄要介于1-200之间");
}
else{
System.out.println("保存年龄成功");
}
}
}

public class AgeIllegalException extends Exception{
public AgeIllegalException()
{
super();
}
public AgeIllegalException(String message)
{
super(message);
}
}
public class demo3 {
public static void main(String[] args) {
//认识自定义异常
try {
saveage(300);
} catch (AgeIllegalException e) {
e.printStackTrace();
}
}
//只要收到年龄小于1岁或大于200岁,就抛出一个异常
public static void saveage(int age) throws AgeIllegalException{
if(age<1||age>200){//年龄不合法
throw new AgeIllegalException("年龄不合法,年龄要介于1-200之间");
}
else{
System.out.println("保存年龄成功");
}
}
}
异常的处理方案
1、底层异常层层往上抛,最外层捕获异常,记录异常信息,响应适当的信息进行提示
public class demo5 {
public static void main(String[] args) {
//异常的处理方案1:底层异常都抛出给最外层调用者,最外层捕获异常,处理异常,响应信息
try {
show1();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void show1() throws Exception {
String str = "2025-09-08 12-13-15";
//把字符串转换成日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date date = sdf.parse( str);
System.out.println(date);
InputStream is = new FileInputStream("d:/abc.txt");
}
}
2、最外层捕获异常后尝试重新修复
public class demo6 {
public static void main(String[] args) {
//异常处理方案2:捕获异常对象,尝试重新修复
//接收用户定价
double price = 0;
while (true) {
price = 0;
try {
price = userInputPrice();
break;
} catch (Exception e) {
//e.printStackTrace();
System.out.println("价格输入有误,请重新输入!");
}
}
System.out.println("价格是:" + price);
}
public static double userInputPrice() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入价格:");
double price = scanner.nextDouble();
return price;
}
}
更多推荐


所有评论(0)