10.16 ppt作业

发布时间 2023-10-19 19:13:37作者: 牟兆迪
  1 //课堂测试
  2 package test01;
  3 
  4 import java.util.Scanner;
  5 
  6 public class Main{
  7 
  8     public static void main(String[] args) {
  9         Scanner scanner = new Scanner(System.in);
 10         while(true){
 11             try{
 12                 String s = scanner.next();
 13                 int num = Integer.parseInt(s);
 14                 int flag = 0;
 15                 if(num>=90&&num<=100){
 16                     flag = 1;
 17                 }
 18                 else if(num>=80&&num<90){
 19                     flag = 2;
 20                 }
 21                 else if(num>=70&&num<80){
 22                     flag = 3;
 23                 }
 24                 else if(num>=60&&num<70){
 25                     flag = 4;
 26                 }
 27                 else if(num>=0&&num<60){
 28                     flag = 5;
 29                 }
 30                 else{
 31                     flag = 0;
 32                 }
 33                 switch(flag){
 34                     case 1:
 35                         System.out.println("优");
 36                         break;
 37                     case 2:
 38                         System.out.println("良");
 39                         break;
 40                     case 3:
 41                         System.out.println("中");
 42                         break;
 43                     case 4:
 44                         System.out.println("及格");
 45                         break;
 46                     case 5:
 47                         System.out.println("不及格");
 48                         break;
 49                     default:
 50                         System.out.println("Error!");
 51                         break;
 52                 }
 53             }
 54             catch(Exception e){
 55                 System.out.println("Error!");
 56             }
 57         }
 58 
 59 
 60     }
 61 }
 62 
 63 //动手动脑
 64 import javax.swing.*;
 65 
 66 class AboutException {
 67     public static void main(String[] a)
 68     {
 69         int i=1, j=0, k;
 70         k=i/j;
 71         try
 72         {
 73 
 74             k = i/j;    // Causes division-by-zero exception
 75             //throw new Exception("Hello.Exception!");
 76         }
 77 
 78         catch ( ArithmeticException e)
 79         {
 80             System.out.println("被0除.  "+ e.getMessage());
 81         }
 82 
 83         catch (Exception e)
 84         {
 85             if (e instanceof ArithmeticException)
 86                 System.out.println("被0除");
 87             else
 88             {
 89                 System.out.println(e.getMessage());
 90 
 91             }
 92         }
 93         finally
 94         {
 95             JOptionPane.showConfirmDialog(null,"OK");
 96         }
 97     }
 98 }
 99 //Exception in thread "main" java.lang.ArithmeticException: / by zero at AboutException.main(Main.java:7)
100 
101 //浮点数除以0
102 public class ThrowDemo {
103     public static void main(String[] args) {
104 //        try {
105         double data = 100 / 0.0;
106         System.out.println("浮点数除以零:" + data);
107 //            if(String.valueOf(data).equals("Infinity"))
108 //            { 
109 //                System.out.println("In Here" ); 
110 //                throw new ArithmeticException("除零异常");
111 //            }
112 //        } 
113 //        catch(ArithmeticException e) { 
114 //            System.out.println(e); 
115 //        } 
116     }
117 }
118 //结果如下:
119 //浮点数除以零:Infinity(随机赋值一个值)
120 
121 //CatchWho.java
122 public class CatchWho {
123     public static void main(String[] args) {
124         try {
125             try {
126                 throw new ArrayIndexOutOfBoundsException();
127             }
128             catch(ArrayIndexOutOfBoundsException e) {
129                 System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch");
130             }
131 
132             throw new ArithmeticException();
133         }
134         catch(ArithmeticException e) {
135             System.out.println("发生ArithmeticException");
136         }
137         catch(ArrayIndexOutOfBoundsException e) {
138             System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch");
139         }
140     }
141 }
142 //结果如下:
143 //ArrayIndexOutOfBoundsException/内层try-catch
144 //发生ArithmeticException
145 
146 //CatchWho2.java
147 public class CatchWho2 {
148     public static void main(String[] args) {
149         try {
150             try {
151                 throw new ArrayIndexOutOfBoundsException();
152             }
153             catch(ArithmeticException e) {
154                 System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
155             }
156             throw new ArithmeticException();
157         }
158         catch(ArithmeticException e) {
159             System.out.println("发生ArithmeticException");
160         }
161         catch(ArrayIndexOutOfBoundsException e) {
162             System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
163         }
164     }
165 }
166 
167 //结果如下:
168 //ArrayIndexOutOfBoundsException/外层try-catch
169 
170 package test01;
171 
172 public class EmbededFinally {
173     
174     public static void main(String args[]) {
175         int result;
176         try {
177             System.out.println("in Level 1");
178             try {
179                 System.out.println("in Level 2");
180                 // result=100/0;  //Level 2
181                 try {
182                     System.out.println("in Level 3");
183                     result=100/0;  //Level 3
184                 }
185 
186                 catch (Exception e) {
187                     System.out.println("Level 3:" + e.getClass().toString());
188                 }
189 
190                 finally {
191                     System.out.println("In Level 3 finally");
192                 }
193                 // result=100/0;  //Level 2
194             }
195 
196             catch (Exception e) {
197                 System.out.println("Level 2:" + e.getClass().toString());
198             }
199             finally {
200                 System.out.println("In Level 2 finally");
201             }
202             // result = 100 / 0;  //level 1
203         }
204         catch (Exception e) {
205             System.out.println("Level 1:" + e.getClass().toString());
206         }
207         finally {
208             System.out.println("In Level 1 finally");
209         }
210 
211     }
212 
213 }
214 //结果如下:
215 /*
216 in Level 1
217 in Level 2
218 in Level 3
219 Level 3:class java.lang.ArithmeticException
220 In Level 3 finally
221 In Level 2 finally
222 In Level 1 finally
223 */
224     
225     
226 package test01;
227 
228 public class SystemExitAndFinally {
229 
230     public static void main(String[] args)
231     {
232         try{
233             System.out.println("in main");
234             throw new Exception("Exception is thrown in main");
235             //System.exit(0);
236         }
237         catch(Exception e)
238         {
239             System.out.println(e.getMessage());
240             System.exit(0);
241         }
242         finally
243         {
244             System.out.println("in finally");
245         }
246     }
247 }
248 //结果如下:
249 /*
250 in main
251 Exception is thrown in main
252 并没有实现finally中的语句,因为System.exit(0);强制程序退出了
253 */