#千锋逆战班 Java

生活中处处充满着温情,只要我们有一双善于发现的明亮的眼睛。

异常复习:
<1>

public static void main(String[] args) {
		Student stu = new Student();
		try {
			stu.setAge(250);//是可能出现异常的代码
		}catch(Exception e) {
			System.err.println(e.getMessage());//只获得报错的原因即可
		} 
		
		try {
			stu.setSex("未知");//受查异常,编译期间就报错,需要处理
		}catch(SexMismatchException se) {//根据方法声明的异常,捕获相应的类型
			System.err.println(se.getMessage());
		}catch(Exception e ) {
			e.printStackTrace();
		}
		
		
//		Class.forName("xxx.xxx");//参数(包名.类名)   可能写错
	}
}
class SexMismatchException extends Exception{
	public SexMismatchException() {}
	public SexMismatchException(String message) {
		super(message);
	}
}
class AgeInputException extends RuntimeException{
	
	public AgeInputException() {}//支持创建无异常原因信息的异常对象
	public AgeInputException(String message) {//提供有参构造方法,支持编写异常原因信息
		super(message);//调用父类的有参构造方法,为message属性赋值。
	}
}
class Student{
	private int age;
	private String sex;
	
	public void setSex(String sex)throws SexMismatchException{
		if(sex.equals("男")||sex.equals("女")) {
			this.sex=sex;
		}else {
			throw new SexMismatchException("性别输入的值为:“男”或者“女”");
		}
	}
	public String getSex() {
		return this.sex;
	}
	public int getAge() {
		return this.age;
	}
	public void setAge(int age){
		if(age > 0 && age < 123) {
			this.age = age;
		}else {
			throw new AgeInputException("年龄的赋值应该在0岁到123岁之间");//抛运行时异常的父类。 不合理。现存的定义好的异常,没有符合现在程序的场景
		}
	}
}

输出参考:
年龄的赋值应该在0岁到123岁之间
性别输入的值为:“男”或者“女”

<2>

public static void main(String[] args) {
		System.out.println(ma());
		System.out.println("请输入您的用户名:");
	}
	public static int ma() {
		int b = 0;
		//读入一个整数b;
		try {
			int n = 100;
			return n / b;
		}catch(Exception e) {
			return 10;
		}finally {
			System.out.println("程序结束了!");
		}
	}
}

输出参考:
程序结束了!
10
请输入您的用户名:

<3>

public class TestOverrideExceptionMethod {

	public static void main(String[] args) {
			Super sup = new Sub();//父类引用指向子类对象  多态
			try {
				sup.method();//在编译期间,调用的父类中声明的方法是有异常的,需要处理
			}catch(Exception e) {
				e.printStackTrace();//处理方案之1:打印堆栈跟踪信息
			}
	}
}
class Super{
	public void method()throws Exception{
		System.out.println("method in  Super");
	}
}
class Sub extends Super{
	public void method()throws ClassNotFoundException,RuntimeException,IOException,SQLException{
		System.out.println("method in  Sub");
	}
}

interface Printable{
	public void print()throws Exception;
}
class MyClass implements Printable{
	public void print() throws ClassNotFoundException,RuntimeException{
		
	}
}

输出参考:
method in  Sub

<4>

	public static void main(String[] args) {
		int result = m1();
		System.out.println(result);
	}
	public static int m1() {
		int a = 10;
		try {
			a = 20;
			throw new RuntimeException();
		}catch(Exception e) {
			a = 30;
			return a;
		}finally {
			a = 40;
		}
	}
}

输出参考:
30

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值