JAVA异常处理初步学习

JAVA的异常处理的try-catch-finally块:

		try{
			可能出错的内容
		}
		catch(异常情况1){
			如果错误1执行的内容
		}
		catch(异常情况2){
			如果错误2执行的内容
		}
		.......
		finally{
			最后无论如何会执行的内容
		}

其中catch如果有包括关系,则应该冲上倒下,范围从小到大。

有一个很经典的例子就是输入两个数算两个数的商:

import java.io.*;
import java.util.*;

public class ExceptionTest {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int a,b,c=0;
		try{
			a=sc.nextInt();
			b=sc.nextInt();
			c=a/b;
		}
		catch(InputMismatchException e){
			System.out.println("您输入的内容不是整形数,是不合法的!");
			c=-1;
			e.printStackTrace();
		}
		catch(ArithmeticException e){
			System.out.println("您输入的除数是0,不合法!");
			c=-2;
			e.printStackTrace();
		}
		catch(Exception e){
			c=-3;
			System.out.println("我是不知名错误");
			e.printStackTrace();
		}
		finally{
			System.out.println("无论如何你一定会看到我的~~~");
		}
		System.out.println(c);
	}
}

另外也可以对一些情况直接抛出异常,但是抛出异常后后面的语句不执行:

public class ExceptionTest {

	public static void main(String[] args) {
		double a=1,b=0;
		System.out.println(divided(a,b));
	}
	public static double divided(double a,double b){
		if(b==0){exception();return -1;}
		else {
			return a/b;
		}
	}
	public static void exception()throws ArithmeticException{
	throw new ArithmeticException();	
	}
}
/*
Exception in thread "main" java.lang.ArithmeticException 
at king.ExceptionTest.exception(ExceptionTest.java:18)
at king.ExceptionTest.divided(ExceptionTest.java:12)
at king.ExceptionTest.main(ExceptionTest.java:9)
*/

其中,JAVA的异常处理内容有:

  •   Exception:顶级异常;
  •   ArithmeticException:算数错误情形,比如说用0作为除数。
  •   ArrayIndexOutOfBoundsException:数组下标越界。
  •   NullPointerException:尝试访问null对象成员。
  •   ClassNotFoundException:不能加载所需类。
  •   InputMismatchException:欲得到的数据类型与实际输入的类型不匹配。
  •   IllegalArgumentException:方法接收到非法参数。
  •   ClassCastException:对象强制类型转换出错。
  •   NumberFormatException:数字格式转换异常,如把“abc”转换成数字

然后,异常情况也可以自定义,下面摸拟一个饭卡存取钱的内容,饭卡钱不能超过1w,不能低于0。

定义的取钱异常类:

public class InsufficientException extends Exception{
	private double money;
	public InsufficientException(double amount)
	{
	 this.money = amount;
	} 
	public double getMoney()
	{
	 return money;
	}
}

定义的存钱异常类:

public class OverFlowException extends Exception{
	private double money;
	public OverFlowException(double amount)
	{
	 this.money = amount;
	} 
	public double getMoney()
	{
	 return money;
	}

}

饭卡信息类:

public class CheckingMoney {
	//money是剩余的钱,id是卡号。
	double money;
	String id;
	public CheckingMoney (String id){
		this.id=id;
	}
	//取钱
	public void drawMoney(double amount)throws InsufficientException{
		if(amount<=money){
			money-=amount;
		System.out.println("您已经成功取"+amount+"元,欢迎下次光临。");	
		}
		else {
			System.out.println("您仅剩"+money+"元。");
			 throw new InsufficientException(amount);
		}
	}
	public void saveMoney(double amount)throws OverFlowException{
		if(money+amount<=10000){
			money+=amount;
			System.out.println("您已经成功存"+amount+"元,欢迎下次光临。");	
		}
		else {
			System.out.println("您还有"+money+"元,最多还可以存"+(10000-money)+"元。");
			throw new OverFlowException(amount);
		}
	}
}

 测试类:

public class Test {
	public static void main(String[] args) {
	    CheckingMoney c = new CheckingMoney("100");
	    try {
			c.saveMoney(50);
		} catch (OverFlowException e) {
			e.printStackTrace();
			System.out.println();
		}
	    try {
			c.drawMoney(99);
		} catch (InsufficientException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			System.out.println();
		}
	    try {
			c.saveMoney(99999);
		} catch (OverFlowException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			System.out.println();
		}
	}
}

运行结果:

您已经成功存50.0元,欢迎下次光临。
您仅剩50.0元。
Excepiton.InsufficientException
您还有50.0元,最多还可以存9950.0元。


	at Excepiton.CheckingMoney.drawMoney(CheckingMoney.java:18)
	at Excepiton.Test.main(Test.java:14)
Excepiton.OverFlowException
	at Excepiton.CheckingMoney.saveMoney(CheckingMoney.java:28)
	at Excepiton.Test.main(Test.java:21)


猜你喜欢

转载自blog.csdn.net/King8611/article/details/81045170
今日推荐