java异常方式一try-catch-finally、finally是可选的、常用的异常对象处理的方式:` ① String getMessage() ② printStackTrace()

1.4try-catch-finally的使用

try{
  		//可能出现异常的代码
  
}catch(异常类型1 变量名1){
 		//处理异常的方式1
}catch(异常类型2 变量名2){
 		//处理异常的方式2
}catch(异常类型3 变量名3){
 		//处理异常的方式3
}
....
finally{
 		//一定会执行的代码
}

1.4.1说明

1.4.1.1.finally
1.4.1.1.1finally是可选的
  • 没有finally
package com.my.java;

import org.junit.Test;

public class FinallyTest {
	@Test
	public void test1() {
		try {
			int a = 10;
			int b = 0;
			System.out.println(a / b);
		}catch(ArithmeticException e) {
			e.printStackTrace();
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		System.out.println("我太厉害了!");
	}

}

在这里插入图片描述

  • 有finally
package com.my.java;

import org.junit.Test;

public class FinallyTest {
	@Test
	public void test1() {
		try {
			int a = 10;
			int b = 0;
			System.out.println(a / b);
		}catch(ArithmeticException e) {
			e.printStackTrace();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			System.out.println("我太厉害了!!!!");
		}
		
//		System.out.println("我太厉害了!");
	}

}

在这里插入图片描述

1.4.1.1.2finally中声明的是一定会被执行的代码。即使catch中又出现异常了,try中有return语句,catch中有return语句等情况。
  • catch中又有异常的情况
package com.my.java;

import org.junit.Test;

public class FinallyTest {
	@Test
	public void test1() {
		try {
			int a = 10;
			int b = 0;
			System.out.println(a / b);
		}catch(ArithmeticException e) {
			e.printStackTrace();
		}catch(Exception e) {
			e.printStackTrace();
			
			int[] arr = new int[10];
			System.out.println(arr[10]);
		}finally {
			System.out.println("我太厉害了!!!!");
		}
		
//		System.out.println("我太厉害了!");
	}
	
	

}

在这里插入图片描述

  • catch里有return
package com.my.java;

import org.junit.Test;

public class FinallyTest {

	@Test
	public void testMethod() {
		int num = method();
		System.out.println(num);
	}
	public int method() {
		try {
			int[] arr = new int[10];
			System.out.println(arr[10]);
			return 1;
		}catch(ArrayIndexOutOfBoundsException e) {
			e.printStackTrace();
			return 2;
		}finally {
			System.out.println("我一定会被执行");
		}
	}
	

}

在这里插入图片描述

1.4.1.1.3像数据库连接、输入输出流、网络编程Socket等资源,JVM是不能自动的回收的,我们需要自己手动的进行资源的释放。此时的资源释放,就需要声明在finally中。
package com.my.java;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.junit.Test;

public class FinallyTest {

	
	@Test
	public void test2() {
		FileInputStream fis = null;
		try {
			File file = new File("hello.txt");
			fis = new FileInputStream(file);
			
			int data = fis.read();
			while(data != -1) {
				System.out.println((char)data);
				data = fis.read();
			}
		}catch(FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(fis != null)//这里必须加这个判断语句,如果不加的话,当fis没有创建对象,他还是null,那么fis不需要close
					fis.close();//这个是必须要手动操作,而且是必须执行
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	

}

在这里插入图片描述

1.4.1.2.使用try将可能出现异常代码包装起来,在执行过程中,一旦出现异常,就会生成一个对应异常类的对象,根据此对象的类型,去catch中进行匹配
1.4.1.3.一旦try中的异常对象匹配到某一个catch时,就进入catch中进行异常的处理。一旦处理完成,就跳出当前的try-catch结构(在没有写finally的情况)。继续执行其后的代码
package com.my.java;

import org.junit.Test;

public class ExceptionTest1 {
	
	@Test
	public void test1() {
		String str = "123";
		str = "abc";
		int num = 0;
		try {
			num = Integer.parseInt(str);
			System.out.println("hello,try");
		}catch(NumberFormatException e){
			System.out.println("出现数值转换异常了。。。。");
		}
		System.out.println("hello,world");
	}

}

在这里插入图片描述

1.4.1.4.常用的异常对象处理的方式: ① String getMessage() ② printStackTrace()
package com.my.java;

import org.junit.Test;



public class ExceptionTest1 {
	
	@Test
	public void test1() {
		String str = "123";
		str = "abc";
		int num = 0;
		try {
			num = Integer.parseInt(str);
			System.out.println("hello,try");
		}catch(NumberFormatException e){
			System.out.println("出现数值转换异常了。。。。");
			//String getMessage():
//			String a = e.getMessage();
//			System.out.println(a);
			//printStackTrace():
			e.printStackTrace();
			
		}
		System.out.println("hello,world");
	}
	

}

在这里插入图片描述

package com.my.java;

import org.junit.Test;



public class ExceptionTest1 {
	
	@Test
	public void test1() {
		String str = "123";
		str = "abc";
		int num = 0;
		try {
			num = Integer.parseInt(str);
			System.out.println("hello,try");
		}catch(NumberFormatException e){
			System.out.println("出现数值转换异常了。。。。");
			//String getMessage():
			String a = e.getMessage();
			System.out.println(a);
			//printStackTrace():
//			e.printStackTrace();
			
		}
		System.out.println("hello,world");
	}
	
	

}

在这里插入图片描述

1.4.1. 5.`catch中的异常类型如果没有子父类关系,则谁声明在上,谁声明在下无所谓。catch中的异常类型如果满足子父类关系,则要求子类一定声明在父类的上面。否则,报错
package com.my.java;

import org.junit.Test;



public class ExceptionTest1 {
	
	@Test
	public void test1() {
		String str = "123";
		str = "abc";
		int num = 0;
		try {
			num = Integer.parseInt(str);
			System.out.println("hello,try");
		}catch(NumberFormatException e){
			System.out.println("出现数值转换异常了。。。。。。");
			//String getMessage():
			String a = e.getMessage();
			System.out.println(a);
			//printStackTrace():
//			e.printStackTrace();
			
		}catch(NullPointerException e) {
			System.out.println("出现空指针异常了。。。。。。");
		}catch(Exception e) {//因为Exception是NumberFormatException和NullPointerException的父类,所以他只能在子类下面否则会报错
			System.out.println("出现异常了。。。。。。");
		}
		System.out.println("hello,world");
	}
	
	

}

在这里插入图片描述

在这里插入图片描述

1.4.1.6.在try结构中声明的变量,再出了try结构以后,就不能再被调用
package com.my.java;

import org.junit.Test;



public class ExceptionTest1 {
	
	@Test
	public void test1() {
		String str = "123";
		str = "abc";
		int num = 0;
		try {
			num = Integer.parseInt(str);
			System.out.println("hello,try");
		}catch(NumberFormatException e){
			System.out.println("出现数值转换异常了。。。。。。");
			//String getMessage():
			String a = e.getMessage();
			System.out.println(a);
			//printStackTrace():
//			e.printStackTrace();
			
		}catch(NullPointerException e) {
			System.out.println("出现空指针异常了。。。。。。");
		}catch(Exception e) {//因为Exception是NumberFormatException和NullPointerException的父类,所以他只能在子类下面否则会报错
			System.out.println("出现异常了。。。。。。");
		}
		
		System.out.println(num);//如果num在try里定义,则这里不能调用num,而且在外面定义时要先初始化
		System.out.println("hello,world");
	}
	
	

}

在这里插入图片描述

1.4.1.7.try-catch-finally结构可以嵌套
package com.my.java;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.junit.Test;

public class FinallyTest {

	@Test
	public void test2() {
		FileInputStream fis = null;
		try {
			File file = new File("hello.txt");
			fis = new FileInputStream(file);
			
			int data = fis.read();
			while(data != -1) {
				System.out.println((char)data);
				data = fis.read();
			}
		}catch(FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(fis != null)//这里必须加这个判断语句,如果不加的话,当fis没有创建对象,他还是null,那么fis不需要close
					fis.close();//这个是必须要手动操作,而且是必须执行
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	

}

在这里插入图片描述

1.4.1.8 体会1

使用try-catch-finally处理编译时异常,是得程序在编译时就不再报错,但是运行时仍可能报错。相当于我们使用try-catch-finally将一个编译时可能出现的异常,延迟到运行时出现。

package com.my.java;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.junit.Test;



public class ExceptionTest1 {
	

	
	@Test
	public void test2() {
		try{
			File file = new File("hello.txt");
			FileInputStream fis = new FileInputStream(file);
			
			int data = fis.read();
			while(data != -1){
				System.out.print((char)data);
				data = fis.read();
			}
			
			fis.close();
		}catch(FileNotFoundException e){//因为FileNotFoundException是IOException的子类,所以必须在IOException的上面
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
	}

}

在这里插入图片描述

1.4.1.9体会2

开发中,由于运行时异常比较常见,所以我们通常就不针对运行时异常编写try-catch-finally了。针对于编译时异常,我们说一定要考虑异常的处理。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Redamancy_06

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值