java手动抛出异常、用户自定义异常类、异常处理5个关键字、throw和throws的区别

1.6手动抛出异常

用throw的时候最常用new Exception或者RuntimeException,要是知道详细的,可以写最详细的

1.6.1代码

package com.my.java2;

public class StudentTest {
	public static void main(String[] args) {
		try {
			Student s = new Student();
			s.register(-1001);
			System.out.println(s);
		} catch (Exception e) {
//			e.printStackTrace();
			System.out.println(e.getMessage());
		}
	}
}
class Student{
	private int id;
	public void register(int id) throws Exception{
		if(id > 0) {
			this.id = id;
		}else {
//			System.out.println("您输入的数据非法!");
			//用throw的时候最常用new Exception或者RuntimeException,要是知道详细的,可以写最详细的
			//手动抛出异常对象
//			throw new RuntimeException("您输入的数据非法!");
			throw new Exception("您输入的数据非法!");
		}
	}
	@Override
	public String toString() {
		return "Student [id=" + id + "]";
	}
	
	
}

1.6.2结果

在这里插入图片描述

1.6.3throw和throws的区别

throw是手动的生成一个异常的对象
throws是处理一个异常

1.7用户自定义异常类

用户自己的异常类必须继承现有的异常类(RuntimeException 、Exception)。一般地,用户自定义异常类都是RuntimeException的子类。
自定义异常类通常需要编写几个重载的构造器。
自定义异常需要提供serialVersionUID
自定义的异常通过throw抛出。
自定义异常最重要的是异常类的名字,当异常出现时,可以根据名字判断异常类型。

package com.my.java2;

public class NegativeException extends RuntimeException{
	
	static final long serialVersionUID = -7034897193246939L;
	
	public NegativeException() {
		
	}
	
	public NegativeException(String message) {
		super(message);
	}

}


测试代码

package com.my.java2;

public class StudentTest {
	public static void main(String[] args) {
		try {
			Student s = new Student();
			s.register(-1001);
			System.out.println(s);
		} catch (Exception e) {
//			e.printStackTrace();
			System.out.println(e.getMessage());
		}
	}
}
class Student{
	private int id;
	public void register(int id) throws Exception{
		if(id > 0) {
			this.id = id;
		}else {
			throw new NegativeException("您输入的数据非法!");
		}
	}
	@Override
	public String toString() {
		return "Student [id=" + id + "]";
	}
	
	
}

在这里插入图片描述

1.8异常处理5个关键字

在这里插入图片描述

1.9综合练习

  • 编写应用程序EcmDef.java,接收命令行的两个参数,要求不能输入负数,计算两数相除。
  • 对数据类型不一致(NumberFormatException) 、 缺少命令行参数(ArrayIndexOutOfBoundsException、 除0(ArithmeticException)及输入负数(EcmDefNegativeException 自定义的异常)进行异常处理。
  • 提示:
  • (1)在主类(EcmDef)中定义异常方法(ecm)完成两数相除功能。
  • (2)在main()方法中使用异常处理语句进行异常处理。
  • (3)在程序中,自定义对应输入负数的异常类(EcDef)。
  • (4)运行时接受参数 java EcmDef 20 10 //args[0]=“20” args[1]=“10”
  • (5)Interger类的static方法parseInt(String s)将s转换成对应的int值。 如:int a=Interger.parseInt(“314”); //a=314;

1.9.1主函数

package com.my.java3;

public class EcmDef {
	public static void main(String[] args) {
		try {
			int i = Integer.parseInt(args[0]);
			int j = Integer.parseInt(args[1]);
			
			int ans = ecm(i, j);
			
			System.out.println(ans);
		}catch(NumberFormatException e) {
			System.out.println("数据类型不一致");
		}catch(ArrayIndexOutOfBoundsException e) {
			System.out.println("缺少命令行参数");
		}catch(ArithmeticException e) {
			System.out.println("除0");
		}catch(EcmDefNegativeException e) {
			System.out.println(e.getMessage());
		}
		
	}
	public static int ecm(int i , int j) throws EcmDefNegativeException{
		if(i < 0 || j < 0) {
			throw new EcmDefNegativeException("分子或分母为负数");
		}
		return i / j;
		
	}
}

1.9.2自定义异常类

package com.my.java3;

public class EcmDefNegativeException extends Exception{
	
	static final long serialVersionUID = -3387516993124229948L;
	
	public EcmDefNegativeException() {
		
	}
	
	public EcmDefNegativeException(String message) {
		super(message);
	}
}

1.10一首小悟结束异常处理

世界上最遥远的距离,是我在if里你在else里,似乎一直相伴又永远分离;
世界上最痴心的等待,是我当case你是switch,或许永远都选不上自己;
世界上最真情的相依,是你在try我在catch。无论你发神马脾气,我都默默承受,静静处理。到那时,再来期待我们的finally。

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Redamancy_06

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值