文章目录
- 1.4try-catch-finally的使用
- 1.4.1说明
- 1.4.1.1.finally
- 1.4.1.2.使用try将可能出现异常代码包装起来,在执行过程中,一旦出现异常,就会生成一个对应异常类的对象,根据此对象的类型,去catch中进行匹配
- 1.4.1.3.一旦try中的异常对象匹配到某一个catch时,就进入catch中进行异常的处理。一旦处理完成,就跳出当前的try-catch结构(在没有写finally的情况)。继续执行其后的代码
- 1.4.1.4.常用的异常对象处理的方式:` ① String getMessage() ② printStackTrace()`
- 1.4.1. 5.`catch中的异常类型如果没有子父类关系,则谁声明在上,谁声明在下无所谓。catch中的异常类型如果满足子父类关系,则要求子类一定声明在父类的上面。否则,报错
- 1.4.1.6.在try结构中声明的变量,再出了try结构以后,就不能再被调用
- 1.4.1.7.try-catch-finally结构可以嵌套
- 1.4.1.8 体会1
- 1.4.1.9体会2
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了。针对于编译时异常,我们说一定要考虑异常的处理。
本文详细介绍了Java中的异常处理机制,特别是try-catch-finally语句块的使用。内容包括finally块的可选性,finally中代码的执行特性,如在catch中处理异常和return语句的情况。此外,还讨论了资源释放的重要性,特别是在处理数据库连接、输入输出流和网络编程资源时。文章通过实例展示了如何匹配和处理不同类型的异常,并探讨了try-catch-finally结构的嵌套使用。最后,作者分享了关于编译时异常和运行时异常处理的体会。
2161





