慕课网 3.1 异常处理

1.try .. catch..finally

import java.util.InputMismatchException;
import java.util.Scanner;
public class Hello {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Hello tct=new Hello();
	/*	int result=tct.test();
		System.out.println("运行结果为:"+result);
		
		int result2=tct.test2();
		System.out.println("我的运行结果为:"+result2);*/
	
		int result3=tct.test3();
		System.out.println("我的运行结果为:"+result3);
	
	}
	/**
	 * 	 * 
	 * @return
	 */
	public int test(){
		int divider=10;
		int result=100;
		try{
			while(divider>-1){
				divider--;
				result=result+100/divider;
			}
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("异常抛出");
			return -1;
		}
		return 0;
	}
	
	/**
	 * 
	 * @return
	 */
	public int test2(){
		int divider=10;
		int result=100;
		try{
			while(divider>-1){
				divider--;
				result=result+100/divider;
			}
			return result;
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("异常抛出2");
			return result=999;
		}finally{
			System.out.println("finally!");
			System.out.println("我是result"+result);
		}
	}
	
	/**
	 * 
	 * @return
	 */
	public int test3(){
		int divider=10;
		int result=100;
		try{
			while(divider>-1){
				divider--;
				result=result+100/divider;
			}			
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("异常抛出3");			
		}finally{
			System.out.println("finally3!");
			System.out.println("我是result"+result);
		}
		System.out.println("运行完毕!");	
		return result;
	}

}

2.throws  throw

import java.util.Scanner;

public class Test {
	private static Scanner console = new Scanner(System.in);

	public static void main(String[] args) {
		//定义”图书“数组
		String[] books = { "C语言", "数据结构", "汇编语言", "高数", "大学语文", "毛概" };
		while (true) {
			System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
			String book;
			try {
				//取得整型命令
				int command = inputCommand();
				//根据不同命令值,进行不同操作
				switch (command) {
				case 1://按照图书名称选择图书
					book = getBookByName(books);
					System.out.println("book:" + book);
					break;
				case 2://按照图书序号(数组下标)选择图书
					book = getBookByNumber(books);
					System.out.println("book:" + book);
					break;
				case -1://返回值为-1,说明输入有误
					System.out.println("命令输入错误!请根据提示输入数字命令!");
					continue;
				default://其他值的命令均认为是错误命令
					System.out.println("命令输入错误!");
					continue;
				}
				break;//退出程序
			} catch (Exception bne) {
				//捕获”图书不存在异常“时,要求重新输入命令
				System.out.println(bne.getMessage());
				continue;
			} 
		}
	}

	//按照图书名称查询图书
	private static String getBookByName(String[] books)
			throws Exception {
		System.out.println("输入图书名称:");
		//获取输入的图书名称
		String name = console.next();
		for (int i = 0; i < books.length; i++) {
			if (name.equals(books[i]))
				//输入的名称与某一图书名称匹配,返回该图书
				return books[i];
		}
		//若无匹配,抛出”图书不存在异常“
		throw new Exception("图书不存在!");
	}

	//根据图书序号(数组下标)查询图书
	private static String getBookByNumber(String[] books)
			throws Exception {
		while (true) {
			System.out.println("输入图书序号:");
			try {
				//获取输入的图书序号(数组下标)
				int index = inputCommand();
				//若返回值为-1
				if(index == -1){
					System.out.println("命令输入错误!请根据提示输入数字命令!");
					continue;
				}
				//若不出现”数组下标越界异常“,则返回相应位置的图书
				String book = books[index];
				return book;
			} catch (ArrayIndexOutOfBoundsException e) {
				//输入的序号不存在(引发”数组下标越界异常“),则抛出”图书不存在异常“
				Exception bookNotExists = new Exception("图书不存在!");
				bookNotExists.initCause(e);
				throw bookNotExists;
			}
		}
	}

	//从控制台输入命令,用于输入命令和输入图书序号
	private static int inputCommand(){
		int command;
		try {
			command = console.nextInt();
			return command;
		} catch (Exception e) {
			//若输入字符型或者字符串,则抛出异常,捕获该异常,抛出”错误命令异常“
			console = new Scanner(System.in);
			//返回-1
			return -1;
		}
	}
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值