JAVA API 异常处理

示例一:
/**
 * java中的异常处理机制
 * @author Administrator
 *
 */
public class ExceptionDemo1 {
	public static void main(String[] args) {
		System.out.println("程序开始了");

		try{
			
			String str = null;
			/*
			 * JVM在执行str.length()方法时发现str为null.
			 * 于是创建一个NullPointerException的实例,并
			 * 将出错的描述信息封装进去,然后将异常抛出来。
			 * 这时JVM会查看下面这句代码有没有被一个try语句
			 * 包围,若没有,说明当前方法没有捕获这个异常的能
			 * 力,那么会将该异常抛出到方法外面,由于抛出到了
			 * main方法外面。JVM会将当前程序终止。
			 * 若有try包围,就会将该空指针异常实例传递给catch
			 * 中的NullpointerException e,然后执行该catch的
			 * 内容用来解决异常。当解决后,程序得以正常继续的
			 * 执行。
			 */
			System.out.println("length:"+str.length());
			
		}catch(NullPointerException e){
			System.out.println("出现了一个空指针!");
		}
		
		System.out.println("程序结束了");
	}
}
/**
 *  多个catch
 * @author Administrator
 *
 */
public class ExceptionDemo2 {
	public static void main(String[] args) {
		System.out.println("程序开始了");
		try{
			String str = "aa";
			System.out.println("length:"+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中去捕获最大的
		 * 异常。这样可以避免由于try中出现了一个没有捕获的
		 * 异常而导致程序中断
		 * 
		 * 在定义多个catch时要注意,若有父子类关系的异常
		 * 同时出现,那么父类异常要定义在子类异常的下面。
		 * 
		 */
		}catch(Exception e){
			System.out.println("反正就是出了个错!");
		}
		
		System.out.println("程序结束了");
	}
}

/**
 * finally块
 * finally可以直接跟在try语句块后面
 * 或者最后一个catch之后
 * finally块中的代码保证我们的程序无论出错与否,否会被
 * 执行。
 * 
 * @author Administrator
 *
 */
public class ExceptionDemo3 {
	public static void main(String[] args) {
		System.out.println("程序开始了");
		
		try {
			
			String str = "";
			System.out.println(str.length());
			System.out.println("!!!!");
			
			
		} catch (Exception e) {
			System.out.println("出错了!");
		} finally{
			System.out.println("finally的代码一定执行!");
		}
		
		System.out.println("程序结束了");
	}
}

/**
 * finally面试题
 * 1:final finally finalize分别是什么?
 *   finalize是Object中定义的一个方法。该方法被jvm执行
 *   当一个对象即将被GC回收前,会调用该方法。
 * 
 * 2:下面的例子
 * 
 * @author Administrator
 *
 */
public class ExceptionDemo4 {
	public static void main(String[] args) {
		System.out.println(
				test("0")+","+test(null)+","+test("")
		);
	}
	
	public static int test(String str){
		try {
			return str.charAt(0)-'0';
		} catch (NullPointerException e) {
			return 1;
		} catch (Exception e){
			return 2;
		} finally{
			return 3;
		}
	}
}

/**
 * throw的作用是主动抛出异常
 * 通常两种情况会抛出异常:
 * 1:满足语法要求,但是不满足业务逻辑
 * 2:出现异常的代码不应当负责处理该异常时(责任问题)
 * 
 * @author Administrator
 *
 */
public class ExceptionDemo5 {
	public static void main(String[] args){
		Person person = new Person();
		/*
		 * 当我们调用一个声明有throws的方法时,编译器要求
		 * 我们必须在调用方法的这里处理该方法可能抛出的异常
		 * 处理手段有两种:
		 * 1:自己try-catch捕获并处理
		 * 2:继续将该异常的抛出声明在当前方法上继续向外抛
		 * 
		 * 永远不要再main方法上声明throws!
		 */
		try {
			person.setAge(10);
		} catch (IllegalAgeException e) {
			e.printStackTrace();
		}
		
		System.out.println("年龄是:"+person.getAge());
		
		
	}
}

示例二:

/**
 * Exception中定义了一些常用方法
 * @author Administrator
 *
 */
public class ExceptionAPIDemo1 {
	public static void main(String[] args) {
		System.out.println("程序开始了");
		try {
			String str = "aa";
			System.out.println(Integer.parseInt(str));
		} catch (Exception e) {
			//输出错误堆栈(程序执行过程,一直到出错的位置)
			e.printStackTrace();
			//String getMessage() 获取错误的信息
			System.out.println(e.getMessage());
		}
		System.out.println("程序结束了");
	}
}

/**
 * Exception的API 2
 * @author Administrator
 *
 */
public class ExceptionAPIDemo2 {
	public static void main(String[] args) {
		try {
			dosome("");
		} catch (Exception e) {
//			e.printStackTrace();
			System.out.println(e.getCause());
		}
	}
	
	
	public static void dosome(String str)throws Exception{
		try {
			System.out.println(str.charAt(0));
		} catch (NullPointerException e) {
			throw new Exception(e);
		} catch(StringIndexOutOfBoundsException e){
			throw new Exception(e);
		}
	}
}
示例三:
/**
 * 自定义异常
 * 该异常表示一个年龄不合法的错误
 * 通常我们在表示当前程序出现的一些业务逻辑错误时,可以
 * 用自定义异常来描述。
 * 
 * @author Administrator
 *
 */
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) {
		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
	}	
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值