4 java exception thrown manually and custom exception class

/*

  • Manually throw an exception: throw (Note that throws exception handling, throw an exception is thrown)
  • 1, Java exception class object to automatically generate except when an exception occurs during the execution of the program by the system and throw, throw and can also be created manually as necessary.
  • 2 first class generates an exception object and then thrown operation (submitted to the Java Runtime Environment) achieved by the throw statement.
  • Must be thrown exception is an instance of a subclass of Throwable or. Otherwise it will produce a syntax error:
  • Typically use RuntimeException, followed by the use of Exception, less other circumstances.
  • Custom exception class:
  • 1, user-defined class is a subclass of RuntimeException.
  • 2, generally require custom exception class constructor write several overloaded.
  • 3, the need to provide custom exception serialVersionUID
  • 4, since the definition of an exception thrown by the throw.
  • 5, custom exception is the name of the most important class exception when an exception occurs, the exception type can be determined according to the name.

*/

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);
	}
	
}
Published 47 original articles · won praise 1 · views 1026

Guess you like

Origin blog.csdn.net/wisdomcodeinside/article/details/104506582