头歌作业-4-2 异常类

目录

第一关

第二关

第三关

第四关

第五关

第六关


第一关

第二关

package step2;

import java.util.Scanner;

public class Task {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num1 = sc.nextInt();
		int num2 = sc.nextInt();
		/********* Begin *********/
		try {
            int res = num1 / num2;
        } catch (ArithmeticException e) {
            System.out.println("除数不能为0");
        }
		
		System.out.println(num1/num2);
		/********* End *********/
	}

}

第三关

package step5;

import java.util.InputMismatchException;
import java.util.Scanner;
public class ExceptionDemo {
    public static void main(String[] args) {
        // *******定义一个静态数组,为确保输出结果正确,请指定数组元素为:{23, 56, 789, -12, 35, 67, 89, 75, 66, 32} ******//
        int[] a = {23, 56, 789, -12, 35, 67, 89, 75, 66, 32};

        // *******调用静态方法search() ,从键盘输入一个索引号,根据索引号查找并输出数组的指定元素******//
       
        // *******捕捉并处理捕捉并处理输入索引号类型不匹配异常( InputMismatchException )和数组下标越界异常(ArrayIndexOutOfBoundsException)******//
        int idx = 0;
        try {
            Scanner sc = new Scanner(System.in);
            idx = sc.nextInt();
            search(a, idx);
        } catch (InputMismatchException i) {
            System.out.println("输入数据类型不匹配null");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("输入的索引号越界" + idx);
        }
        System.out.println("查找到的元素是:" + search(a, idx));
    }  
   // *******定义静态方法search(int [] a,int x),实现根据索引号x,在数组a中查找指定下标的元素并返回该元素
    static int search(int[] a, int x) {
        return a[x];
    }
}

第四关

package step3;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Task {
	/********* Begin *********/
	//请在合适的部位添加代码
	public static void main(String[] args) throws FileNotFoundException {	
		test();
	}
	public static void test() throws FileNotFoundException {
		File file = new File("abc");
		if(!file.exists()){		//判断文件是否存在
			//文件不存在,则 抛出 文件不存在异常
			throw new FileNotFoundException("该文件不存在");
		}else{
			FileInputStream fs = new FileInputStream(file);
		}
	}
	/********* End *********/
}



第五关

package step4;

import java.util.Scanner;

public class Task {
	/********* Begin *********/
	public static void main(String[] args) throws MyException {
		Scanner sc = new Scanner(System.in);
		String username = sc.next();

		//判断用户名
		if (username.length() < 3) {
            throw new MyException("用户名小于三位Exception");
        }
        System.out.println("用户名格式正确");
	}
}

class MyException extends Exception {
    private static final long serialVersioUID = 1L;

    public MyException(){

    }
    public MyException(String msg) {
        super(msg);
    }
}

/********* End *********/

第六关

自定义异常类:NoThisSongException

package step6;

public class NoThisSongException extends Exception{
   // *******自定义播放异常类***********//

   NoThisSongException(){}
   
   NoThisSongException(String msg) {
      super(msg);
   }
}

Player

package step6;

public class Player {
   // ********定义方法play(int index),根据指定的索引号播放对应歌曲,若索引号>10,则抛出“NoThisSongException”异常**********//
    
    static void player(int index) throws NoThisSongException {
       if (index <= 10) {
          System.out.println("正在播放第" + index + "首歌曲");
       } else if (index > 10){
         throw new NoThisSongException("您播放的歌曲不存在");
       }
    }
    
} 

PlayerTest

package step6;

import java.util.Scanner;

public class PlayerTest {
    public static void main(String[] args) {
    //******* 创建Player对象,并调用player()方法,捕获并处理该方法可能产生的异常 ********//
        Scanner sc = new Scanner(System.in);
        int index = sc.nextInt();
        try {
            Player.player(index);
        } catch (NoThisSongException e) {
            System.out.println(e.getMessage());
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值