异常的处理

java中异常的处理

异常处理:
try-catch-finally(真正处理异常)
throws+异常类型(只是将异常抛出,并未将异常解决 )
抓抛类型::过程一:“抛”:程序在正常执行过程中,一旦出现异常,就会在异常代码
处生成一个对应的异常类的对象,并将此对象抛出;一旦抛出异常,其后代码不在执行
过程二:“抓”:可以理解为异常的处理方式;①try-catch-finally ②throws
try-catch-finally的使用:
try{
可能出现异常的代码
}catch(异常类型1 变量名1){
处理异常1
}catch(异常类型2 变量名2){
处理异常2
}catch(异常类型3 变量名3){
处理异常3
}
。。。
finally{
一定会执行的代码
}
说明:1、finally是可选的
2、使用try将可能出现异常的代码包起来,在执行过程中,一旦出现异常,就会生成一个对应异常类的对象根据此类对象的类型去catch中去匹配
3、一旦catch中匹配到某一个catch时,就会进入catch中执行异常的处理,一旦完成处理,就会跳出当前的try-catch结构继续执行其后的代码(没有finally的情况)。
4、catch中的异常类型如果没有子父类的关系,声明在前在后都可以;如果满足父子类关系,则子类在上,父类在下
5、常用的异常处理方法,①String getMessage() ②printStackTrace

package test1;

public class ExceptionTest1 {
	public static void main(String[] args) {
		String str = "123";
		str = "abc";
		try {
			int num = Integer.parseInt(str);
			System.out.println("hello---1");
		}catch (NumberFormatException e) {
			System.out.println("数值转换异常");
		}catch (NullPointerException e) {
			// TODO: handle exception
			System.out.println("空指针异常");
		}
		System.out.println("hello---2");
	}
}

在这里插入图片描述

finally是可选的,finally中声明的


public class FinallyTest2 {
	

	public static void main(String[] args) {
		int num = method();
		System.out.println(num);
	}
		
	
	@SuppressWarnings("finally")
	public static  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("一定会被执行");
			return 3;
		}
	}
}

在这里插入图片描述
finally的使用:
finally是可选的;finally中声明的一定会被执行的代码,及时catch中又出现了异常,try中有return语句,catch中有return语句等情况;像数据库连接,数据输入输出流
,网络编程Socket资源等,JVM是不能自动回收的,需要手动的显式的进项资源的释放,此时的资源释放就需要声明在finally中;

package test1;

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

import org.junit.Test;

//hello boy



public class FinallyTest1 {
	@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.print((char)data);
				data = fis.read();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(fis != null) {
					fis.close();
				}
			}catch(IOException e) {
				e.printStackTrace();
			}
		}
	}
}


throws类型
throws+异常类型 写在异常方法的声明处,指明此方法执行时可能会抛出的异常类型;一旦当方法体执行时,出现异常,仍会在异常代码处生成一个异常对象,此对象满足throws后异常类型时,就会被抛出。异常后续的代码就不在执行。

package test1;

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

public class ExceptionTest4 {
	
	public static void main(String[] args) {
		try {
			method2();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	public static void method2() throws FileNotFoundException, IOException {
		method1();
	}
	
	
	public static void method1() throws FileNotFoundException,IOException {
		File file = new File("hello1.txt");
		FileInputStream fis = new FileInputStream(file);
		
		int data = fis.read();
		while (data != -1) {
			System.out.print((char)data);
			data = fis.read();
		}
		fis.close();
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值