栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

文章目录
    • 1.6手动抛出异常
      • 1.6.1代码
      • 1.6.2结果
      • 1.6.3throw和throws的区别
    • 1.7用户自定义异常类
    • 1.8异常处理5个关键字
    • 1.9综合练习
      • 1.9.1主函数
      • 1.9.2自定义异常类
    • 1.10一首小悟结束异常处理

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。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023229.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号