Java 第九周总结

1. 本周学习总结

2. 书面作业

1.常用异常

1.1 截图你的提交结果(出现学号)

1.2 自己以前编写的代码中经常出现什么异常、需要捕获吗(为什么)?应如何避免?

  • 以前的代码经常出现空指针的,需要捕获。能在代码编译的时候提示错误点,而且代码也会继续往下执行,不会因为这一个问题而影响到后面的内容。

1.3 什么样的异常要求用户一定要使用捕获处理?

  • 用户在程序运行时触发所导致的错误,需要异常机制来捕捉和处理。
  • 当前函数无法将某种状态传递到外面的时候也要用异常处理

2.处理异常使你的程序更加健壮

2.1 截图你的提交结果(出现学号)

2.2 实验总结

  • 这一题的关键在于如何捕获异常之后,不会影响到正常的代码程序运行,继续之前未完成的赋值。
  • 所以这里我把异常判断放在了for循环赋值的内部。如果当前赋值操作出现异常,捕获后进行处理。
  • 这时的catch处理异常,然后继续执行接下来的代码,也就是for循环。那么本该在这里被赋值的数,就为0
  • 所以为了能够返回当前错误赋值的位置,重新赋值,i--


3.throw与throws

3.1 截图你的提交结果(出现学号)

  • 未达成预计结果,提交未通过

public class Main3 {
	public static void main(String[] arge) throws IllegalArgumentException{
		Scanner in= new Scanner(System.in);
		int n=in.nextInt();
		int[] arr=new int[n];
		double[] brr=new double[n];
		for(int i=0;i<n;i++){
			arr[i]=in.nextInt();
		}		
		int begin=in.nextInt();
		int end=in.nextInt();
		for(int i=0;i<n;i++)
			brr[i]=(double)arr[i];
		try{
			System.out.println(ArrayUtils.findMax(brr, begin, end));
		}catch(IllegalArgumentException e){
			System.out.println(e);
		}
	}
		
}

class ArrayUtils{
	public static double findMax(double[] arr,int begin,int end) throws IllegalArgumentException{
		try{
			if(begin>=end)
				throw new IllegalArgumentException("begin:"+begin+">=:"+end);
			else if(begin<0)
				throw new IllegalArgumentException("begin:"+begin+"<0");
			else if(end>arr.length)
				throw new IllegalArgumentException("end:"+end+">arr.length");
			/*else{
				for(;arr[begin]<arr[begin=1]&&begin<end;begin++)
					arr[begin]=arr[begin+1];				
			}	*/						
		}catch(Exception e){
				
		}
		return arr[begin];
	}
}

4.函数题

4.1 截图你的提交结果(出现学号)

4.2 一个try块中如果可能抛出多种异常,捕获时需要注意些什么?

  • 捕获时,要注意catch后面括号内的,如果是(Exception e)说明接受多种异常状态。
Unreachable catch block for Exception. It is already handled by the catch block for Exception  
  • 异常的catch catch块。它已经被捕获提单处理

  • 所以,最好是为不同的异常状态做不同的处理,方便区分和捕获

  • 另外,一旦某个catch捕获到匹配的异常类型,将进入异常处理代码。一经处理结束,就意味着整个try-catch语句结束。其他的catch子句不再有匹配和捕获异常类型的机会。

5.为如下代码加上异常处理

byte[] content = null;
FileInputStream fis = new FileInputStream("testfis.txt");
int bytesAvailabe = fis.available();//获得该文件可用的字节数
if(bytesAvailabe>0){
    content = new byte[bytesAvailabe];//创建可容纳文件大小的数组
    fis.read(content);//将文件内容读入数组
}
System.out.println(Arrays.toString(content));//打印数组内容

5.1 改正代码,让其可正常运行。注意:里面有多个方法均可能抛出异常

try{
			byte[] content = null;
			FileInputStream fis = new FileInputStream("testfis.txt");
			int bytesAvailabe = fis.available();//获得该文件可用的字节数
			if(bytesAvailabe>0){
			    content = new byte[bytesAvailabe];//创建可容纳文件大小的数组
			    fis.read(content);//将文件内容读入数组
			}
			System.out.println(Arrays.toString(content));//打印数组内容
		}
		catch(FileNotFoundException e){
			System.out.println("文件未找到");
		}
		catch(IOException e){
			System.out.println("文件输入有误");
		}
		catch(ArrayIndexOutOfBoundsException e){
			System.out.println("数组越界");
		}

5.2 如何使用Java7中的try-with-resources来改写上述代码实现自动关闭资源?

6.重点考核:使用异常改进你的购物车系统举至少两个例子说明你是如何使用异常机制让你的程序变得更健壮。说明要包含2个部分:1. 问题说明(哪里会碰到异常)。2.解决方案(关键代码)

package shopping;

import java.util.ArrayList;



class ShoppingCartException extends Exception
{
 
  public ShoppingCartException(String s)
  {
        super(s);
  }

}

public class ShoppingCart{
	 
	private static int totalnum=0;
	private static double totalprice=0;
	private int cnum;
	private String name;
	private double price;
	private String category;
	private int num;
	
	public ArrayList<Goods> cart=new ArrayList<Goods>();
	public ArrayList<ShoppingCart> cartlist=new ArrayList<ShoppingCart>();
	Goods good;
	
	public ShoppingCart(String category, String name, double price,int num,int cnum)throws ShoppingCartException {
		setCategory(category);
		setName(name);
		setPrice(price);
		setCnum(cnum);
		setTotalprice(cnum);
		setTotalnum(cnum);
		good=new Goods(category,name,price ,num);
		if(num<cnum) throw new ShoppingCartException("选择数量大于库存!");
		cart.add(good);
	}
	
	public static void setTotalprice(double totalprice) {
		ShoppingCart.totalprice = totalprice;
	}
	
	

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public int getCnum() {
		return cnum;
	}

	public void setCnum(int cnum) {
		this.cnum = cnum;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public int getTotalnum() {
		return totalnum;
	}

	public void setTotalnum(int cnum) {
		this.totalnum = cnum+this.totalnum;
	}

	public double getTotalprice() {
		return totalprice;
	}

	public void setTotalprice(int cnum) {
		this.totalprice = cnum*this.getPrice()+this.totalprice;
	}


	@Override
	public String toString() {
		return getCategory()+"   名称:" + getName() + " 	价格:" + getPrice() +" 	数量:"+cnum;
	}
	public static String total()
	{
		return "	总价:"+totalprice+"		总数:"+totalnum;
		
	}

}

3. 码云上代码提交记录

原文地址:https://www.cnblogs.com/ycll/p/6733386.html