• JAVA课程作业07


    • 动手动脑:请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识
       1 import javax.swing.*;
       2 
       3 class AboutException {
       4    public static void main(String[] a) 
       5    {
       6       int i=1, j=0, k;
       7     //  k=i/j;
       8 
       9     try
      10     {        
      11         k = i/j;    // Causes division-by-zero exception
      12                     //throw new Exception("Hello.Exception!");
      13     }
      14     
      15     catch ( ArithmeticException e)
      16     {
      17         System.out.println("被0除.  "+ e.getMessage());
      18         //public String getMessage():返回此 throwable 的详细消息字符串。 
      19     }
      20     
      21     catch (Exception e)
      22     {//Exception 类,万能的,放在最后.
      23     
      24     //boolean result = object(任意对象表达式) instanceof class(任意已定义的对象类)
      25     //如果 object 是 class 的一个实例,则 instanceof 运算符返回 true。
      26     //如果 object 不是指定类的一个实例,或者 object 是 null,则返回 false。
      27         if (e instanceof ArithmeticException)
      28             System.out.println("被0除");
      29         else
      30         {  
      31             System.out.println(e.getMessage());
      32             
      33         }
      34     }
      35     
      36     finally
      37      {
      38              JOptionPane.showConfirmDialog(null,"OK");
      39      }
      40         
      41   }
      42 }

             

    • Ø 阅读以下代码(CatchWho.java),写出程序运行结果:
     1 public class CatchWho { 
     2     public static void main(String[] args) { 
     3         try { 
     4             try { 
     5                 throw new ArrayIndexOutOfBoundsException(); 
     6             } 
     7             catch(ArrayIndexOutOfBoundsException e) { 
     8                System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch"); 
     9             }
    10  
    11             throw new ArithmeticException(); 
    12         } 
    13         catch(ArithmeticException e) { 
    14             System.out.println("发生ArithmeticException"); 
    15         } 
    16         catch(ArrayIndexOutOfBoundsException e) { 
    17            System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 
    18         } 
    19     } 
    20 }

     

     1 public class CatchWho2 { 
     2     public static void main(String[] args) { 
     3         try {
     4             try { 
     5                 throw new ArrayIndexOutOfBoundsException(); 
     6             } 
     7             catch(ArithmeticException e) { 
     8                 System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); 
     9             }
    10             throw new ArithmeticException(); 
    11         } 
    12         catch(ArithmeticException e) { 
    13             System.out.println("发生ArithmeticException"); 
    14         } 
    15         catch(ArrayIndexOutOfBoundsException e) { 
    16             System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 
    17         } 
    18     } 
    19 }

     1 public class SystemExitAndFinally {
     2 
     3     public static void main(String[] args)
     4     {
     5         try{
     6 
     7             System.out.println("in main");
     8             throw new Exception("Exception is thrown in main");
     9             //System.exit(0);
    10 
    11         }
    12         catch(Exception e)
    13         {
    14             System.out.println(e.getMessage());
    15             //System.exit(0);
    16         }
    17         finally
    18         {
    19             System.out.println("in finally");
    20         }
    21     }
    22 
    23 }

    不会,System.exit(0);可以终止程序。

    Ø编写一个程序,此程序在运行时要求用户输入一个  整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。
    Ø要求程序必须具备足够的健壮性,不管用户输入什么样的内容,都不会崩溃。
     1 import java.util.Scanner;
     2 
     3 public class Marks {
     4     public static void main(String args[]){
     5         int marks;
     6         Scanner input=new Scanner(System.in);
     7             System.out.println("请输入成绩(0~100):");
     8         try
     9         {
    10             String str = input.nextLine();//以字符串的形式输入
    11             marks = Integer.parseInt(str);//将字符串状转化为整形
    12             //marks=input.nextInt();//无法识别空格,换行
    13             if(marks<=100&&marks>=90) System.out.println("优");
    14             if(marks<90&&marks>=80)    System.out.println("良");
    15             if(marks<80&&marks>=70)    System.out.println("中");
    16             if(marks<70&&marks>=60)    System.out.println("及格");    
    17             if(marks<60&&marks>=0)    System.out.println("不及格");
    18             
    19             if(marks<0||marks>100) throw new Exception();
    20         }
    21         catch(Exception e)
    22         {
    23             System.out.println("输入格式错误!");
    24         }
    25         
    26     }
    27 }

  • 相关阅读:
    HDU 1059 Dividing(多重背包)
    新华网,要厚道
    js与DOM初步:访问html元素
    Hacker News网站的文章排名算法工作原理
    做技术,是个精益求精的事情
    百度贴吧客户端(Android)网络通信行为分析
    Storm同时接收多个源(spout和bolt)
    各种流处理系统的比较
    Kafka学习
    关联规则推荐及Apriori算法
  • 原文地址:https://www.cnblogs.com/ljz-2014/p/4967628.html
Copyright © 2020-2023  润新知