异常

/*Error
* Exception:运行时异常(),编译时异常();
* 异常Exception
* 对一段代码进行监控
* try{
* 要监控的代码
* }catch(异常){
* 出现了异常后的处理
* }
*
* */

package com.ly.cn01;

import org.junit.Test;

public class Dome {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub

		/*Error
		 * Exception:运行时异常(),编译时异常();
		 * 异常Exception
		 * 对一段代码进行监控
		 * try{
		 * 要监控的代码
		 * }catch(异常){
		 * 	出现了异常后的处理
		 * }
		 * 
		 * */
//		try {
//			method3();
//		} catch (Exception e) {
//		
//			System.out.println("异常了。。。");
//		}
		method4();
//		method5(null);
		
	}
	@Test
	public void method1(){
		try{
			System.out.println("回家过年");
			System.out.println("吃好吃的");
			Object s=null;
			s.toString();
	//		int i=5/0;
			System.out.println("玩好玩的");
			System.out.println("********");
		}catch(ArithmeticException e){
			e.printStackTrace();
			System.out.println("封层");
		}catch(NullPointerException e){
			e.printStackTrace();
			System.out.println("击毙");
		}finally{
			System.out.println("饭还是要吃的");
		}
	}
	@Test
	public void method2(){
		try{
			Class.forName("");
		}catch(ClassNotFoundException e){
			e.printStackTrace();
			System.out.println("乱棍打死");
		}
	}
	public static void method3(){
		try {
			System.out.println(5/0);
		} catch (Exception e) {
			System.out.println("自己犯错自己承担");
		}
	}
//	throws 把异常往上跑,谁调用谁处理。
	public static void method4() throws Exception{
		Class.forName("");
	}
//	throw 方法体中主动抛出一个异常
	public static void method5(Object o){
		if(o==null){
			throw new NullPointerException("参数不能为null");
		}
		System.out.println("正常功能。。");
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值