文章目录
一.什么是异常?
异常概念: 程序在编译或在运行时出现的例外情况。
父类: Throwable:Exception和Error
Exception和Error的区别:
Error 是不可避免的,比如说内存溢出
Exception 是可以避免的,比如解析异常、空指针异常等等…
如图:
异常包括: 运行时异常RuntimeException和非运行是异常(编译器异常) 父类为: Exception
异常有什么影响: JVM执行程序时,如果程序出现异常,会导致JVM停止程序的运行
1.常见的异常
(1)运行时异常
空指针异常:
public class NullPointExceptionDemo {
public static void main(String[] args) {
String s=null;
// s=new String("abcd");
s.indexOf(2);
System.out.println("end");
}
}
数组下标越界议程
public class ArrayIndexOutofBoundsExceptionDemo {
public static void main(String[] args) {
int[] a= {1,2,3};
System.out.println(a[3]);
System.out.println("end");
}
}
算术异常
public class ArithMaticExceptionDemo {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
System.out.println(10/i);
System.out.println("end");
}
}
类型转换异常
public class ClassCastExceptionDemo {
public static void main(String[] args) {
//
// Object o=new Object();
// String s=(String)o;
// System.out.println(s);
String s="abcd";
Object o=s;
Integer in=(Integer)o;
System.out.println(in);
System.out.println("end");
}
}
(2)非运行时异常
例如:解析异常
import java.text.SimpleDateFormat;
import java.util.Date;
public class ParseExceptionDemo {
public static void main(String[] args) {
String s="1999-9-9";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
// 如果你调用了一个可能出现异常的方法或构造器,编译不通过。
// 解析方法,该方法可能出现解析异常
Date d=sdf.parse(s);
System.out.println(d);
}
}
2.异常的处理
(1)声明抛出异常:throws
案例:
public class ParseExceptionDemo {
public static void main(String[] args) throws ParseException {
String s="1999-9-9";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
// 如果你调用了一个可能出现异常的方法或构造器,编译不通过。
// 解析方法,该方法可能出现解析异常
Date d=sdf.parse(s);
System.out.println(d);
}
}
(2)捕获处理异常:try-catch-finally
案例:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ParseExceptionDemo2 {
public static void main(String[] args) {
String s="1999-9-9";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
// 如果你调用了一个可能出现异常的方法或构造器,编译不通过。
// 解析方法,该方法可能出现解析异常
Date d=null;
try {
System.out.println("try begin");
d = sdf.parse(s);
System.out.println("try end");
} catch (ParseException e) {
//e.printStackTrace();
System.out.println("catch处理捕获的异常");
}
System.out.println(d);
}
}
import java.util.Scanner;
public class CatchMoreDemo {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
String s=null;
int[] aa= {1,2,3};
try {
int a=10/i;
//s.indexOf(2);
System.out.println(aa[3]);
}catch (ArithmeticException e) {
System.out.println("算数异常");
}catch (NullPointerException e) {
System.out.println("空指针");
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组下标越界异常");
}catch (Exception e) {
System.out.println("其他异常");
}finally {
//在任何情况下都会执行,最后收尾工作
System.out.println("fffff");
}
System.out.println("end");
}
}
public class TestFinnally {
public static int test(int i) {
try {
int a=10/i;
return a;
}catch (Exception e) {
System.out.println("算数异常");
return 0;
}finally {
System.out.println("end");
return 5;
}
}
public static void main(String[] args) {
int t1=test(0);
System.out.println(t1);
int t2=test(1);
System.out.println(t2);
}
}
3.自定义异常(了解)
步骤:
1. 继承Exception或者RuntimeException
2. 编写构造器,指定异常信息
3. 使用异常
案例:银行存钱和取钱
1. 继承Exception或者RuntimeException
public class BalanceNotEnoughException extends Exception{
public BalanceNotEnoughException() {
super("余额不足");
}
}
2. 编写构造器,指定异常信息
public class Account {
private int id;
private String name;
private double balance;
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 double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Account() {
// TODO Auto-generated constructor stub
}
//构造器
public Account(int id, String name, double balance) {
super();
this.id = id;
this.name = name;
this.balance = balance;
}
public void in(double money) {
balance=balance+money;
}
//指定异常信息,当我们取钱的时候才会出现余额不足的情况
public void out(double money) throws BalanceNotEnoughException {
if(balance<money) {
//余额不足
throw new BalanceNotEnoughException();
}else {
balance-=money;
}
}
}
3. 使用异常
public class TestBalanceNotEnough {
public static void main(String[] args) {
Account ac=new Account(622848001, "张三", 100);
try {
ac.out(1000);
} catch (BalanceNotEnoughException e) {
//e.printStackTrace();
}
System.out.println("end");
}
}