JAVA异常课后作业_java异常处理课后作业

java把可能会发生错误的代码放进try语句块中,catch语句块中的代码用于处理错误,不管是否有异常发生,finally语句块中的语句始终保证被执行。如果没有提供合适的异常处理代码,JVM会结束掉整个应用程序。

2、动手动脑

eec02583d552ce00370c77a7014d82cb.png

源码

public class CatchWho {

public static void main(String[] args) {

try {

try {

throw new ArrayIndexOutOfBoundsException();

}

catch(ArrayIndexOutOfBoundsException e) {

System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch");

}

throw new ArithmeticException();

}

catch(ArithmeticException e) {

System.out.println("发生ArithmeticException");

}

catch(ArrayIndexOutOfBoundsException e) {

System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch");

}

}

}

结果

ArrayIndexOutOfBoundsException/内层try-catch

发生ArithmeticException

720b122ee430bb691b7e6bc079adc8b0.png

源码

public class CatchWho2 {

public static void main(String[] args) {

try {

try {

throw new ArrayIndexOutOfBoundsException();

}

catch(ArithmeticException e) {

System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");

}

throw new ArithmeticException();

}

catch(ArithmeticException e) {

System.out.println("发生ArithmeticException");

}

catch(ArrayIndexOutOfBoundsException e) {

System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");

}

}

}

结果

ArrayIndexOutOfBoundsException/外层try-catch

3、动手动脑

5249db2af754b16963a29cb80c798672.png

源码

public class EmbededFinally {

public static void main(String args[]) {

int result;

try {

System.out.println("in Level 1");

try {

System.out.println("in Level 2");

// result=100/0;  //Level 2

try {

System.out.println("in Level 3");

result=100/0;  //Level 3

}

catch (Exception e) {

System.out.println("Level 3:" + e.getClass().toString());

}

finally {

System.out.println("In Level 3 finally");

}

// result=100/0;  //Level 2

}

catch (Exception e) {

System.out.println("Level 2:" + e.getClass().toString());

}

finally {

System.out.println("In Level 2 finally");

}

// result = 100 / 0;  //level 1

}

catch (Exception e) {

System.out.println("Level 1:" + e.getClass().toString());

}

finally {

.    System.out.println("In Level 1 finally");

}

}

}

结果

in Level 1

in Level 2

in Level 3

Level 3:class java.lang.ArithmeticException

In Level 3 finally

In Level 2 finally

In Level 1 finally

4、动手动脑

e790dab08f6dd68fd3ab2a1283032346.png

源码

public class SystemExitAndFinally {

public static void main(String[] args)

{

try{

System.out.println("in main");

throw new Exception("Exception is thrown in main");

//System.exit(0);

}

catch(Exception e)

{

System.out.println(e.getMessage());

System.exit(0);

}

finally

{

System.out.println("in finally");

}

}

}

结果

in main

Exception is thrown in main

当存在try中有throw new Exception()时,finally不会执行

课堂测验

输入整数判断成绩等级,抛出异常

import java.util.Scanner;

class MyException extends Exception

{

public MyException(String messege)

{

super(messege);

}

}

class numbertest //自定义异常类

{

public int score(int a) throws MyException  //当a<0或a>100时,抛出一个自定义异常

{

if(a<0 || a>100)

{

throw new MyException("成绩输入有误");//抛出异常

}

return a;//返回a

}

}

public class test1{

public static void main(String[] args) throws MyException

{

try

{

Scanner scan=new Scanner(System.in);

System.out.println("请输入分数");

int n = 0,i;

n=scan.nextInt();

numbertest k=new numbertest();

try

{

int t=k.score(n);

i=n/10;

switch(i)

{

case 10:

case 9:

System.out.println("优");break;

case 8:

System.out.println("良");break;

case 7:

System.out.println("中");break;

case 6:

System.out.println("及格");break;

default:

System.out.println("不及格");break;

}

}

catch(MyException e)

{

System.out.println(e);//输出

}

}

catch(Exception e)//由于变量定义为int型,所以输入字符时,则输出该异常信息

{

System.out.println("输入格式不合法");//输出

}

}

}

结果

1553bae2628d7cef9e7f6308cdb91ffb.png

b6cada49e5d2f2752a3bef08226389c8.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值