异常的体系结构

编译时异常和运行时异常

/**
 * 
 * 编译时异常
 * IOException
 * FileNotFoundException
 * ClassNotFoundException
 *          
 * 
 * 运行时异常RuntimeException
 * NullPointException
 * ArrayIndexOutOfBoundsException
 * ClassCastException
 * NumberFormatException
 * InputMismatchException
 * ArithmeticException
 *
 */

try catch finally的捕获

package com.up;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;

/**
 * try catch finally 解决异常 编译异常时异常已经能够确定具体是哪个错,catch抛出的异常必须要对应
 * catch异常具有return效果,如果说异常已经抛出后续层级结构的异常将不会输出 ,catch异常层级结构必须从小到大
 * 
 */
public class Main002 {

	
	//========================try catch解决编译时异常(真下是用于编辑时异常:a.txt已经不在代码的控制范围内了)=================================
	@Test
	public void method001() {
		File file = new File("b.txt");
		try {
			FileInputStream fis = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			//异常自定义输出方式
			System.out.println("FileNotFoundException");
		}catch(IOException e){
			System.out.println("IOException");
		}
		catch (Exception e) {
			System.out.println("Exception");
		}
	}

	
	//==========================try catch解决运行时异常(不建议用于运行时异常:100a还在代码的控制范围内)====================================
	@Test
	public void method002() {
		
		String str = "100a";
		//String str = null;
		
		try {
			int num = Integer.parseInt(str);
			//str.charAt(0);
		} 
		catch(NumberFormatException e){
			//异常输出方式一
			//e.printStackTrace();
			//异常输出方式二
			System.out.println(e.getMessage());
		}
		catch(NullPointerException e){
			//e.printStackTrace();
			System.out.println(e.getMessage());
		}
		catch (Exception e) {
			//e.printStackTrace();
			System.out.println(e.getMessage());
		} 

		System.out.println("用药过量");
	}
}
/**
 * finally是可选的
 * finally是一定会执行的代码
 * 
 * finally使用场合:
 * 1.catch中又出现异常
 * 2.try中有return语句,catch中有return
 * 3.像数据库,输入输出流,网络编程socket等资源,JVM是不能自动的回收的,我们需要自己手动的进行资源的释放,此时的资源释放,就需要声明在finally中
 *
 */

throws的使用

package com.up;

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

import org.junit.Test;


/**
 * main已经到了Java程序的入口了,如果还继续throws只能抛给JVM虚拟机程序报错,所以说main不建议抛出异常的
 * 
 * throws使用场合:连续的多个方法互相调用存在递进关系建议throws抛出异常这种写法
 */
public class Main004 {
	
	
	public static void main(String[] args)   {
		try {
			method002();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	
	@Test
	public static void method002() throws FileNotFoundException{
			method001();
	}
	
	
	@Test
	public static void method001() throws FileNotFoundException{
		File file = new File("b.txt");
		FileInputStream fis = new FileInputStream(file);
	}
}

package com.up;

import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 方法重写的规则之一:
 * 子类重写的方法抛出的异常类型不大于父类被重写的方法抛出的异常类型
 */
public class Main005 {
	

	
	public static void main(String[] args) {
		Student student = new Student();
		try {
			student.method001();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}


class Person{

	public void method001() throws IOException{
		System.out.println("Person method001");
	}
}


class Student extends Person{
	
	public void method001() throws FileNotFoundException{
		System.out.println("Student method001");
	}
}

自定义异常

package com.up;

/**
 * 如何自定义异常类?
 * 1.继承于现有的异常结构:RuntimeException,Exception
 * 2.提供全局常量:serialVersionUID
 * 3.提供重戴的构造器
 */
public class MyException extends Exception{

	public MyException(){
		
	}
	
	
	public MyException(String msg){
		super(msg);
	}
	

}
package com.up;

/**
 * throw手动抛出异常
 *
 */
public class Person {
	
	int id;
	
	public void method001(int id) throws Exception{
		if(id>0){
			this.id = id;
		}else{
			//System.out.println("您输入的数据非法!");
			
			//throw new RuntimeException("您输入的数据非法!");
			//throw new Exception("您输入的数据非法!");
			//抛出空指针跟不能为负数业务不匹配
			//throw new NullPointerException("空指针,业务跟不能为负数不匹配!");
			//错误的
			//throw new String("不能输入负数");
			
			//抛出自定义异常类
			throw new MyException("自定义MyException异常类");
		}
	}

	public String toString() {
		return "Person [id=" + id + "]";
	}

}


package com.up;

public class Main001 {
	
	public static void main(String[] args) {
		Person person = new Person();
		try {
			person.method001(-1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println(person);
		
	}

}

发布了93 篇原创文章 · 获赞 31 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_43866567/article/details/97490839