第七章编程题&补充题

不是很理解题目意思
课本P117T6
按照题意在catch两种异常后先打印对应中文提示貌似就可以了。


public class book0706{
	public static void main(String[] args)
	{	
		int a,b,c;
        a=67; b=0; 
		try{
			c=a/b;
			System.out.println(a+"/"+b+"="+c);
		}
		catch(ArithmeticException e){
			System.out.println("除数为零异常!");
			e.printStackTrace();
		}
		catch(NullPointerException e){
			System.out.println("空指针异常!");
			e.printStackTrace();
		}
	}
}

T7

import java.util.Scanner;

public class book0707 {
	public static void main(String args[]){
		Scanner input;
		int e=0;
		String cin;
		String[] arr;
		double[] ina=new double[10];
		double max=0.0,min=0.0;
		System.out.print("请");
		p:do{
			System.out.println("输入10个数:");	
			input=new Scanner(System.in);
			cin=input.nextLine();
			arr=cin.split(" ");	
			if(arr.length!=10){
				System.out.print("数量错误!重新");
				arr=null;
				continue;
			}
			for (int i=0;i<10;i++){
				try{
					double value = Double.valueOf(arr[i]);
					ina[i]=value;
					if(i==0){
						max=ina[i];min=ina[i];
					}else{
						if(min>ina[i])min=ina[i];
						if(max<ina[i])max=ina[i];
					}
				}
				catch(Exception e1){
					System.out.print("类型错误!重新");
					arr=null;
					continue p;
				}
			}
			e=1;
		}while(e==0);
		System.out.println("MAX:"+max);
		System.out.println("MIN:"+min);
	}
}

附加题
1.
Error和Exception是平行类。Error是所有错误类的祖先类,Exception是所有异常类的祖先类。Error不是程序需要捕获与进行处理的,当Error发生时,程序会立即停止;Exception有许多子类,课本P109图7.3列出了这些子类在不同包中的一些概要分布。

异常处理包括声明抛出处理和程序捕获处理
声明抛出处理:
隐式声明抛出:异常类型是RuntimeException或是其子类,程序方法对这类异常不作任何声明抛出或者处理,直接交给调用该方法的地方处理,且程序并不会编译失败也不会对产生异常的代码给出提示。课本例7.2-7.5
显式声明抛出:public static void main(String args[]) throws IOExceprion
调用main方法的是JVM,JVM进行默认处理
程序捕获处理:
嵌套或非嵌套都是通过try-catch捕获异常。例7.7-7.8

import java.util.EmptyStackException;
import java.util.Stack;

public class eddit0703 {
	private static int[] x;
	public static void main(String[] args)
	{	
		try{			//ArrayIndexOutOfBoundsException
  	 		String foo = args[1];	
  			 System.out.println("foo = " + foo);}
  	 	catch(ArrayIndexOutOfBoundsException e){
  	 		System.out.println("catch ArrayIndexOutOfBoundsException");
  	 	}finally{
			
		}
		ArithmeticE();
		EmptyStackE();
		NullPointerE();
		NegativeArraySizeE();

	}
	public static void ArithmeticE() {
		int a,b,c;
		a=67; b=0;
		try{
			c=a/b;	}
		catch(ArithmeticException e){
			System.out.println("catch ArithmeticException");
		}finally{
			
		}
	}
	public static void EmptyStackE() {
  	 	Stack st = new Stack();
  	 	try{
  	 	Object ob = st.pop(); }
  	 	catch(EmptyStackException e){
  	 		System.out.println("catch EmptyStackException");
  	 	}finally{
			
		}
	}
	public static void NullPointerE() {
  	 	try{
  			System.out.println(x[1]);}
  	 	catch(NullPointerException e){
  	 		System.out.println("catch NullPointerException");
  	 	}finally{
			
		}
	}
	public static void NegativeArraySizeE() {
        try{
            int[] a = new int[-3];
        } catch (NegativeArraySizeException nase) {
            System.out.println("catch NegativeArraySizeException");
        } finally{
			
		}
    }
}

7.4


public class edit0704 extends Exception{
	edit0704(String msg){
		super(msg);   
	}
	static void throwOne(int a) throws edit0704
	{
		if(a!=20)
		throw new edit0704("error");
	}
	public static void main(String args[])
	{
		try
		{throwOne(52);}
		catch(edit0704 e)
	    {e.printStackTrace();}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值