Exception

package exception;
/**
 * java异常捕获机制中的try-catch
 * 
 * 语法:
 * try{
 *  代码片段
 *  }catch(XXException e){
 *  捕获try代码片段中出现的XXException并处理
 *  }
 * @author LIDN1601806300
 *
 */
public class TryCatchDemo {
	public static void main(String[] args) {
		System.out.println("程序开始了");
		try{
			String str = "a";
		/*
		 * 当JVM运行程序时发现出现了某个异常时
		 * 会自动实例化该类型的异常实例,并将代码
		 * 执行过程设置到该异常实例中将其抛出
		 * 
		 * 若抛出异常的代码没有被异常处理机制包含
		 * 那么JVM会将该异常抛到当前方法之外,若
		 * 抛出到main方法之外,则当前程序中断
		 * (单线程情况下)
		 */
		System.out.println(str.length());
		//try中出错代码以后的内容都不会在执行
		System.out.println(str.charAt(0));
		System.out.println(Integer.parseInt(str));
		System.out.println("!!!!");
		}catch(NullPointerException e) { //try中不出错那么catch中代码就不会执行
			System.out.println("出现了空指针!");
		}catch(StringIndexOutOfBoundsException e) {
			System.out.println("下标越界");
			/*
			 * 养成一个好习惯,在最后一个catch捕获
			 * Exception,防止因为未捕获的异常导致程序
			 * 中断
			 */
		}catch(Exception e) {
			System.out.println("反正是出错了");
		}
		/*
		 * 异常捕获应当子类放上面捕获父类在下面捕获
		 */
		
		System.out.println("程序结束了");
	}
}
package exception;
/**
 * 异常常用方法
 * @author LIDN1601806300
 *
 */
public class ExceptionAPIDemo {
	public static void main(String[] args) {
		System.out.println("程序开始了");
		try {
			String str = "a";
			System.out.println(Integer.parseInt(str));
		}catch(Exception e) {
			System.out.println("程序出错了");
			//输出错误堆栈信息
			e.printStackTrace();
			//输出错误消息
			String message = e.getMessage();
			System.out.println("message:"+message);
		}
		System.out.println("程序结束了");
	}

}
package exception;
/**
 * 使用当前类测试异常地抛出
 * @author LIDN1601806300
 *
 */
public class Person {
	private int age;

	public int getAge() {
		return age;
	}
	/**
	 * 一个方法内部使用throw抛出一个异常,就要在
	 * 方法上使用throws声明改异常地抛出以告知调用者
	 * 处理这个异常
	 * 只有RuntimeException及其子类型异常在方法中
	 * 抛出是不必要求必须在方法上声明该异常地抛出,其他
	 * 类型异常则是必须的,否则编译不通过
	 * @param age
	 * @throws Exception
	 */

	public void setAge(int age) throws IllegalAgeException {
		if(age<0||age>100) {
			//取值不在合理区间,抛异常
			throw new IllegalAgeException ("年龄不合法");
		}
		this.age = age;
	}
	
	
}
package exception;
/**
 * 年龄不合法异常
 * 
 * 自定义异常(需要继承一个父类,然后重写父类的构造方法),通常用于描述业务逻辑错误
 * @author LIDN1601806300
 *
 */
public class IllegalAgeException extends Exception{
	private static final long serialVersionUID = 1L;
	
	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
	}

	public static void main(String[] args) {
		
	}
}
package exception;
/**
 * 异常地抛出
 * 
 * throw关键字,用于将一个异常抛出
 * 通常两种情况会主动抛出一个异常:
 * 1:程序遇到一个满足语法要求,但是不满足业务逻辑
 * 要求的时候可以主动抛出一个异常给调用者
 * 2:程序出现了异常,但是不应当在当前代码片段中解决
 * 该异常时可以抛出给调用者
 * @author LIDN1601806300
 *
 */
public class ThrowDemo {
	public static void main(String[] args) {
		Person p = new Person();
		/*
		 * 语法满足,但是不满足业务逻辑要求
		 */
			/*
			 * 当我们调用一个含有throws声明异常抛出的方法
			 * 时,编译器要求我们必须处理这个异常,处理方式有两种:
			 * 1:try-catch自行捕获并处理
			 * 2:在当前方法上继续使用throws声明
			 * 将该异常抛出	
			 */
		
			try {
				p.setAge(10000);
			} catch (IllegalAgeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		System.out.println("年龄:"+p.getAge());
	}
}
package exception;

import java.awt.AWTException;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 子类重写父类含有throws声明抛出异常的方法时
 * 对throws的重写原则
 * @author LIDN1601806300
 *
 */
public class ThrowsDemo {
	public void dosome()
			throws IOException,AWTException{
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
class Son extends ThrowsDemo{
	//public void dosome()
	//		throws IOException,AWTException{
	//	
	//}
	//可以不再抛任何异常
	//public void dosome(){
	//	
	//	}
	//可以仅抛出部分异常
	//public void dosome()throws IOException{
	//	
	//}
	//可以抛出父类方法中抛出异常的子类型异常
	//public void dosome()throws FileNotFoundException{
	//	
	//}
	
	//不允许抛出额外异常
	//public void dosome()throws SQLException{
	//	
	//}
	//不允许抛出父类方法抛出异常的父类型异常
	//public void dosome() throwsException{
	//	
	//}

}








package exception;
/**
 * finally块
 * finally块只能定义在异常处理机制的最后,可以直接跟
 * 在try后面或者最后一个catch之后
 * 
 * finally可以保证只要程序运行到try当中,那么无论
 * try当中的代码片段是否出现异常,finally块里面的
 * 代码都必然执行
 * 通常把释放资源等操作放在finally中,比如流的关闭
 * 
 * @author LIDN1601806300
 *
 */
public class FinallyDemo {
	public static void main(String[] args) {
		System.out.println("程序开始了");
		try {
			String str = "";
			System.out.println(str.length());
		}catch(Exception e){
			System.out.println("出错了");
		}finally {
			System.out.println("finally中的代码运行了");
		}
		System.out.println("程序结束了");
	}
}
package exception;

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

/**
 * 流操作中的异常捕获
 * @author LIDN1601806300
 *
 */
public class FinallyDemo2 {
	public static void main(String[] args) {
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("fos.txt");
			int d = fis.read();
			System.out.println(d);
			} catch (IOException e) {
				
			}finally {
				try {
					if(fis!=null) {
						fis.close();
					}
				}catch(IOException e) {
					
				}
			}
	}
}
package exception;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * JDK1.7之后推出了一个新的特性:自动关闭
 * @author LIDN1601806300
 *
 */
public class FinallyDemo3 {
	public static void main(String[] args) {
		try(
				/*
				 * AutoCloseable接口的子类可以定义在这里
				 * 
				 * 自动关闭语法是编译器认可,编译后的class
				 * 文件中流的关闭还是在finally中进行的
				 */
			FileInputStream fis = new FileInputStream("fos.txt");	
				){
			int d = fis.read();
			System.out.println(d);
		}catch(IOException e) {
			
		}
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值