异常:程序执行过程中发生不正常行为称为异常

示例: 1️⃣ 算术异常(ArithmeticException):
public static void main(String[] args) {
System.out.println(1/0);
}
2️⃣空指针异常(NullPointerException)
public static void main(String[] args) {
int[] a=null;
System.out.println(a[0]);
}
3️⃣数组下标越界异常(ArrayIndexOutOfBoundsException)
public static void main(String[] args) {
int[] a={1,2,3,4};
System.out.println(a[5]);
}
注意:

根据发生的时机不同分类:

异常的分类根据发生时机的不同,分为编译时异常和运行时异常。
编译时异常:在程序编译期间发生的异常称为编译时异常,也可以称为受查异常。
示例:

运行时异常:在程序运行时期间发生的异常,也称为非受检查异常。
示例:
public static void main(String[] args) {
System.out.println(10/0);
}
异常处理的五个关键字:throw,throws,try,catch,finally
异常的抛出:可以理解为自己手动抛出异常。
示例:
public static void main(String[] args) {
//如果4%2==0为真,那么抛出异常
if (4%2==0){
throw new ArithmeticException();
}
}结果:




throws:用于声明可能会抛出的异常,当方法中抛出编译时异常,如果用户不想处理异常,可以交给JVM处理,也可以提醒方法的调用者处理异常。
> throws的语法格式:
> 修饰符 返回值类型 方法名(参数列表)throws 异常类型1,异常类型2...{
.......
>}示例: 像之前的不克隆异常

声明当前可能抛出的异常,我们无需再处理,但这里的异常没有被处理,只是交给了JVM处理

- try-catch-finally的语法格式:
try{
//可能出现异常的代码
}catch(异常1 e){
//捕获异常1
}catch(异常2 e){
//捕获异常2
}finally{
//资源的释放
}public static void main(String[] args) {
try{
System.out.println(10/0);
}catch(Exception exception){
System.out.println("捕获运行时异常");
}
}
public static void main(String[] args) {
try{
System.out.println(10/0);
}catch(ArithmeticException e){
//捕获异常
System.out.println("捕获算术异常");
}
//捕获异常成功,其后面的代码可以执行
System.out.println("1111");
} public static void main(String[] args) {
try{
System.out.println(10/0);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("捕获");
}
}
public static void main(String[] args) {
try{
throw new ArithmeticException();
}catch(ArithmeticException e){
System.out.println("捕获");
}
System.out.println("111");
} public static void main(String[] args) {
try{
System.out.println(10/0);
}catch(ArithmeticException e){
throw new ArithmeticException();
}
System.out.println("111");
}既然catch写多个可能出现的异常,那么会抛出多个异常吗?我们来看一下
public static void main1(String[] args) {
//同时抛出多个异常
try{
System.out.println("111");
int[] a={1,2,3};
System.out.println(a[4]);
System.out.println("123");
System.out.println(10/0);
}catch(ArithmeticException e){
System.out.println("捕获到算术异常");
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("捕获到数组越界异常");
}
System.out.println("1111");
}
父类可以捕获子类的异常吗❓ 瞅瞅下面的代码👀,运行时异常(RuntimeException)是算术异常的父类,而Exception是所有类的父类:
public static void main(String[] args) {
try{
System.out.println(10/0);
}catch(RuntimeException e){
System.out.println("捕获运行时异常");
}
try{
System.out.println(10/0);
}catch(Exception e){
System.out.println("捕获异常");
}
}
答案显而易见是可以的✔️。既然如此Exception是所有异常的父类时可以捕获所有异常的;那Exception的父类Throwable也可以捕获异常吗❓


2.父类在前,子类在后

public static void main(String[] args) {
try{
System.out.println("111");
int[] a={1,2,3};
System.out.println(a[4]);
System.out.println("123");
System.out.println(10/0);
}catch(ArithmeticException | ArrayIndexOutOfBoundsException e){
System.out.println("捕获到异常");
}
System.out.println("1111");
}如上代码所示:

咦,看了这么多,我发现了,只要抛出了异常,后面的代码就无法执行了,那如果我有一定要执行的语句呢,应该怎么办❓这个时候就要finally派上用处了 finally
finally:finally是对在程序正常或异常退出时,必须对资源进行回收。并且如果因为异常,有些执行语句,执行不到,可以用finally来解决。并且可以执行必要的清理操作,保持程序的一致性和稳定性。

自定义异常:我们自己定义维护符合我们实际需要的异常结构。
示例:我们实现一个用户登入功能,我们需要输入用户名和密码,在输入过程中,我们可能会出现错误的情况,用我们之前学的知识写一下,代码如下:
class Login{
private String userName;
private String passWord;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
//登入
public void loginof(String userName,String passWord){
if(!this.userName.equals(userName)){
System.out.println("用户名错误!");
}
if(!this.passWord.equals(passWord)){
System.out.println("密码错误!");
}
}
}
public class Test {
public static void main(String[] args) {
Login login=new Login();
login.setUserName("a");
login.setPassWord("111");
login.loginof("a","123");
}
}
他的密码输入错误,结果输出如上。如果我们想要在我们输错时报出异常,这时,我们可以自己定义一个异常,用户名输入错误异常和密码输入错误异常;
我们可以定义一个输入错误异常类和密码输入错误异常类来继承运行时异常
public class UserNameException extends RuntimeException{
public UserNameException(){
super();
}
public UserNameException(String m){
super(m);
}
}public class PassWordException extends RuntimeException{
public PassWordException(){
super();
}
public PassWordException(String m){
super(m);
}
}抛出异常
public void loginof(String userName,String passWord){
if(!this.userName.equals(userName)){
throw new UserNameException("用户名错误");
}
if(!this.passWord.equals(passWord)){
throw new PassWordException("密码错误!");
}
}
}