java的异常处理

  1. java异常捕获机制中的try-catch,try块是用来扩上可能出错的代码片段,catch块是用来捕获try块中代码抛出的错误,并解决。
public static void main(String[] args) {
		System.out.println("程序开始了");
		try{
		String str="";
		/*
		 * JVM在执行代码的过程中若发现了一个异常,则会实例化这种情况的异常实例,并将代码的
		 * 整个执行过程完整设置到异常中来表示该错误报告,设置完毕后将该异常抛出。
		 * 若抛出异常的这句代码有try包围,则JVM会检查catch中是否有关注该异常,关注则交给catch
		 * 并解决,否则会将异常抛出到当前方法外,由调用当前方法的代码解决该异常。
		 */
		System.out.println(str.length());
		System.out.println(str.charAt(0));
		System.out.println(Integer.parseInt(str));
		
		}catch(NullPointerException e){
			System.out.println("出现了空指针");
		}catch(StringIndexOutOfBoundsException e){
			System.out.println("字符串下标越界了");
			/*
			 * 应当养成一个好习惯,在最后一个catch中捕获Exception.避免因未捕获异常导致程序中断
			 * 
			 * 当多个catch捕获不同异常时,这些异常间存在继承关系,
			 * 那么子类异常要在上,先行捕获,父类异常在下
			 */
		}catch(Exception e){//Exception 可以捕获一切错误
			System.out.println("反正就是出了个错");
		}
		
		
		System.out.println("程序结束了");

	}

  1. finally块,finally块定义在异常捕获机制的最后,可以直接跟在try块之后或者最后一个catch之后。finally块中的代码一定执行,无论try块中的代码是否抛出异常,所以通常会把释放资源等操作放在finally中,例如关闭流。
public static void main(String[] args) {
		System.out.println("程序开始了");
		try {
			String str="";
			//String str=null;
			System.out.println(str.length());
		} catch (Exception e) {
			System.out.println("出错了");
		}finally{
			System.out.println("finally中的代码执行了");
		}
		System.out.println("程序结束了");
	}

  1. finally对于流的处理
public static void main(String[] args) {
		BufferedReader br=null;
		try {
			br=new BufferedReader(new InputStreamReader(new FileInputStream("src/day08/ExceptionDemo3.java")));
			String line=null;
			while((line=br.readLine())!=null){
				System.out.println(line);
			}
		} catch (Exception e) {
			System.out.println("出错了");
		}finally{
			if(br!=null){
				try {
					br.close();
				} catch (IOException e) {
				}	
			}
			
		}

	}

  1. 测试异常的抛出
/**
 * 此类用来测试throw与throws
 * @author aa
 *
 */
public class Person {
	private int age;

	public int getAge() {
		return age;
	}
	/**
	 * 当一个方法中使用throw抛出一个异常时,就要在方法上使用throws声明该类异常的抛出以通知调用者解决。
	 * 只有RuntimeException及其子类异常使用throw抛出时不强制要求必须使用throws声明,
	 * 其它异常都是强制要求的,否则编译不通过。
	 * @param age
	 * @throws Exception
	 */
	public void setAge(int age) throws IllegalAgeException{
		if(age<0||age>100){
			//throw后面应该要跟一个异常的实例
			throw new IllegalAgeException("年龄不合法");
		}
		this.age = age;
	}
	
}

public static void main(String[] args) {
			Person p=new Person();
			/*
			 * 当调用一个含有throws声明异常抛出的方法时,编译器要求必须处理该异常,处理手段两种:
			 * 1.使用try-catch捕获并处理
			 * 2.在当前方法上继续使用throws声明该异常的抛出
			 * 如何选择:该归我管我就try-catch,不该我管我就继续throws;不能在main方法上throws
			 */
			
				try {
					p.setAge(200); 
				} catch (IllegalAgeException e) {
					
					e.printStackTrace();
				}
		
			System.out.println("年龄为:"+p.getAge());

	}

  1. 重写父类一个含有throws异常抛出声明的方法时,子类该方法的throws的重写原则
public class ExceptionDemo5 {

	public void dosome() throws IOException,AWTException{
		
	}

}

class Son extends ExceptionDemo5{
	//以下1,2,3,是允许的
	/*1.不再抛出任何异常
	public void dosome(){
		
	}*/
	
	
	/*2.可仅仅抛出父类方法中抛出的部分异常
	public void dosome() throws IOException{
		
	}*/
	
	
	/*3.允许抛出父类方法抛出异常的子类异常
	public void dosome() throws FileNotFoundException{
		
	}*/
	
	
	//以下4,5是不允许的
	/*4.不允许抛出额外异常
	public void dosome() throws SQLException{
		
	}*/
	
	
	/*5.不允许抛出父类方法抛出异常的父类异常
	public void dosome() throws Exception{
		
	}*/
	
	
}

  1. Exception常用方法
public static void main(String[] args) {
		System.out.println("程序开始了");
		try{
		String str="abc";
		System.out.println(Integer.parseInt(str));
		}catch(Exception e){
			//e.printStackTrace();
			System.out.println(e.getMessage());
		}
		System.out.println("程序结束了");
	}
  1. 自定义异常,通常是用来描述 某个业务逻辑上出现的问题。自定义异常的名字应当做到见名知义.
    年龄不合法异常
public class IllegalAgeException extends Exception{
	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {
		

	}
// source--Generate Constructor from superClass
	
	
	public IllegalAgeException() {
		super();
		// TODO Auto-generated constructor stub
	}

	public IllegalAgeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
		super(message, cause, enableSuppression, writableStackTrace);
		// TODO Auto-generated constructor stub
	}

	public IllegalAgeException(String message, Throwable cause) {
		super(message, cause);
		// TODO Auto-generated constructor stub
	}

	public IllegalAgeException(String message) {
		super(message);
		// TODO Auto-generated constructor stub
	}

	public IllegalAgeException(Throwable cause) {
		super(cause);
		// TODO Auto-generated constructor stub
	}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值