throws自定义抛出异常




package 异常处理;

class inrangeexception extends Exception{
	
	inrangeexception(int wrongnum){
		super(wrongnum + "is not a validate num");
		System.out.println("exception was throwed ");
	}
}


class validation {
	
	public void inrange(int num) throws Exception{
		if(num > 0 && num < 1000){
			 System.out.println(num + "  in the range");
		}
		else{
			throw new inrangeexception(num);
		}
	}
}




package 异常处理;

import java.util.Scanner;
public class test_inrange {
	
	
	
	public static void main(String[] args) {
		
		validation v = new validation();
		int num = 0;
		Scanner sc = new Scanner(System.in);
		num  = sc.nextInt();
		try {
			v.inrange(num);
		} catch (Exception e) {
			
			System.out.println(e.getMessage());
			//如果此处添加 return ,永远别想输出 num 到屏幕上
			//reason:catch块中添加 return ,则 catch 之后的语句将无法显示
		}
		System.out.println("the later scentence in the programe " + num);
		
	}

}


/***********************************************************************
 * *********************************************************************
//异常抛出:
45444
exception was throwed 
45444is not a validate num
the later scentence in the programe 45444
 * **********************************************************************
 * **********************************************************************
//无异常情况:
4
4  in the range
the later scentence in the programe 4

 * **********************************************************************
 * *********************************************************************/





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值