java异常4 手动抛出异常与自定义异常类

/*

  • 手动抛出异常:throw(注意throws是处理异常,throw是抛出异常)
  • 1,Java异常类对象除在程序执行过程中出现异常时由系统自动生成并抛出,也可根据需要使用人工创建并抛出。
  • 2首先要生成异常类对象,然后通过throw语句实现抛出操作(提交给Java运行环境)。
  • 可以抛出的异常必须是Throwable或其子类的实例。否则将会产生语法错误:
  • 通常使用RuntimeException,其次使用Exception,其他情况较少。
  • 自定义异常类:
  • 1,用户自定义异常类都是RuntimeException的子类。
  • 2,自定义异常类通常需要编写几个重载的构造器。
  • 3,自定义异常需要提供serialVersionUID
  • 4,自定义的异常通过throw抛出。
  • 5,自定义异常最重要的是异常类的名字,当异常出现时,可以根据名字判断异常类型。

*/

package exception;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Exception_Throw {
  public static void main(String[] args) {
	  Scanner scan = new Scanner(System.in);
	  Worker w = new Worker();
	for(;;) {  //通过for循环,持续输入数据,直到数据合法不抛出异常,则会break跳出循环
	try {
	    System.out.println("请输入工资");
		int num = scan.nextInt();
		w.getPay(num);//出现异常会跳转到catch中,break不会执行。
		System.out.println(w);
		break;//不出异常,执行到break,跳出循环,取得正常数据
	 } catch (InputMismatchException e) {
		System.out.println(e.getMessage());
		System.out.println("请重新输入");
	 } catch (RuntimeException e) {
		System.out.println(e.getMessage());//getMessage()方法返回的String数据就是手动创建异常对象时,输入的语句
		System.out.println("请重新输入");
	}
	}
	
	for(;;) { 
		try {
			System.out.println("请输入今天的得分");
			int num = scan.nextInt();
			w.setScore(num);
			System.out.println("今天的得分是" + w.score);
			break;
		 } catch (InputMismatchException e) {
			System.out.println(e.getMessage());
			System.out.println("请重新输入");
		 }
		 catch (NumberOutOfBoundsException e) {
			System.out.println(e.getMessage());
			System.out.println("请重新输入");
		 }
	}
	
	for (; ;) {
		//练习,两个数字相除的异常处理
		try {
			System.out.println("请输入分子");
			int a = scan.nextInt();
			System.out.println("请输入分母");
			int b = scan.nextInt();
			int c = ecm(a, b);
			System.out.println(a + "除以" + b + "等于" + c);
			break;
		} catch (InputMismatchException e) {
			System.out.println("数据类型错误,请重新输入");
		} catch (NegativeNumberException e) {
			System.out.println(e.getMessage());
			System.out.println("请重新输入");
		} catch (ArithmeticException e) {
			System.out.println("分母不能为0 ,请重新输入");
		} 
	}
		
		System.out.println("-----------");
}
  
  public static int ecm(int i,int j) throws NegativeNumberException {
	  if(i < 0 || j < 0) {
		 throw new NegativeNumberException("输入数据不能为负数") ;
	  }
	  return i / j;
  }
  
}

class Worker{
	int score;
	int salary;
	static int minSalary = 2000;
	
	void getPay(int i){//这里的throws是处理异常
		if(i >= minSalary) {
		salary = i;
		}else {
			//System.out.println("数据非法");//如果不加throw依然会被赋值
			//throw new String("数据非法");//抛出的异常必须是Throwable或其子类的实例
			throw new RuntimeException("低于法定最低工资,数据非法");//这里的throw是抛出异常
		}
	}
	@Override
	public String toString() {
		return "Worker [salary=" + salary + "]";
	}
	
	void setScore(int i){
		if(i >= 0 && i <= 100) {
		score = i;
		}else {
			throw new NumberOutOfBoundsException("数据范围必须在0-100之间");
		}
	}
		
}

class NumberOutOfBoundsException extends RuntimeException{
	static final long serialVersionUID = 13265653435L;
	
	public NumberOutOfBoundsException(){
		super();
	}
	
	public NumberOutOfBoundsException(String message) {
		super(message);
	}
	
}

class NegativeNumberException extends Exception{
	static final long serialVersionUID = 1365653435L;
	
	public NegativeNumberException(){
		super();
	}
	
	public NegativeNumberException(String message) {
		super(message);
	}
	
}
发布了47 篇原创文章 · 获赞 1 · 访问量 1026

猜你喜欢

转载自blog.csdn.net/wisdomcodeinside/article/details/104506582