java给成绩加异常处理_java异常处理

一.动手动脑

1)代码

import javax.swing.*;

class AboutException {

public static void main(String[] a)

{

double i=-1, j=0, k;

k=i/j;

try

{

k = i/j; // Causes division-by-zero exception

//throw new Exception("Hello.Exception!");

}

catch ( ArithmeticException e)

{

System.out.println("被0除. "+ e.getMessage());

}

catch (Exception e)

{

if (e instanceof ArithmeticException)

System.out.println("被0除");

else

{

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

}

}

finally

{

JOptionPane.showConfirmDialog(null,"OK "+k);

//JOptionPane.showInternalConfirmDialog(null, k);

}

}

}

二.使用Java异常处理机制

注意throw和catch的对应关系,一个抛出必须有一个catch

三.动手动脑

1)源代码

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");

}

}

}

3)结果分析

抛出两次异常,先处理第一次再处理第二次。

四.动手动脑

1)源代码

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");

}

}

}

2)截图

c7dbd8cffc89ced3f4c12b065e2fe09b.png

3)结果分析

一个throw和catch后,才能执行下一个异常

五.动手动脑

1)源代码

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");

}

}

}

2)结果截图

89d9ba8294b3cefbffbda0cdbbcae684.png

六.动手动脑

1)源程序

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");

}

}

}

2)结果截图

d0869131ff01df2f58d8b7d22f305751.png

七.动手动脑

编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。

要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃。

1)源代码

import java.util.*;

public class Prograss {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

String pro;

System.out.println("请输入成绩:");

while(true){

try{

pro=sc.nextLine();

if(pro.matches("\\D*")){

throw new InException("输入有误!不是的不是数字!");

}

else{

int proo=Integer.parseInt(pro);

System.out.print("输入正确!");

judge(proo);

break;

}

}

catch(InException e){

System.out.print("请重新输入:");

}

}

sc.close();

}

public static void judge(int pro){

if(pro>=0&&pro<60)

System.out.println("成绩不合格!");

else if(pro<70)

System.out.println("成绩合格!");

else if(pro<80)

System.out.println("成绩中!");

else if(pro<90)

System.out.println("成绩良!");

else if(pro<=100)

System.out.println("成绩优!");

else

System.out.println("输入成绩不符");

}

}

class InException extends Exception{//

public InException(String msg){

super(msg);

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值