关键词:
动手动脑1:请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识。
源码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import javax.swing.*; class AboutException public static void main(String[] a)
int 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 = i / j;
知识:
1.Try //可能发生运行错误的代码; catch(异常类型 异常对象引用) //用于处理异常的代码 finally //用于“善后” 的代码
2.使用Java异常处理机制
把可能会发生错误的代码放进try语句块中。 当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。 catch语句块中的代码用于处理错误。 当异常发生时,程序控制流程由try语句块跳转到catch语句块。 不管是否有异常发生,finally语句块中的语句始终保证被执行。 如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。
动手动脑2:阅读以下代码(CatchWho.java),写出程序运行结果:
源代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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
动手动脑3:写出CatchWho2.java程序运行的结果
源代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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
动手动脑4:当有多个嵌套的try…catch…finally时,要特别注意finally的执行时机。 请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。 特别注意: 当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。
源代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
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" );
|
运行结果:
经过多次修改result = 100 / 0;的位置,可以得知将会跳过剩下的所有直到遇到第一个catch处理错误。继续运行剩下的。
动手动脑5:辨析:finally语句块一定会执行吗? 请通过 SystemExitAndFinally.java示例程序回答上述问题
源代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
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" );
|
运行结果:
System.exit(0);可以结束一切但是不可以阻止catch异常。
课后作业:编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。 要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃。
源代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
package T9; import java.util.Scanner; class MyException extends Exception //输入异常
public MyException()
public MyException(String msg)
super (msg);
public class Score public static void main(String[] args) Scanner s = new Scanner(System.in); String str = null ; double score = 0 ; boolean flag = false ; //判断输入格式正确 System.out.print( "请输入成绩(0-100):" ); while (!flag)
try
str = s.next(); for ( int i = 0 ;i < str.length();i++) //判断输入的是不是double
if (str.length() == 1 && str.charAt(i) == ‘.‘ ) throw new MyException( "数字格式异常!" ); if ((str.charAt(i) >= ‘0‘ && str.charAt(i) <= ‘9‘ ) || str.charAt(i) == ‘.‘ ) ; else throw new MyException( "数字格式异常!" ); if (i == str.length() - 1 )
score = Double.parseDouble(str); if (score > 100.0 || score < 0.0 ) throw new MyException( "数字超出范围!" ); else flag = true ;
catch (MyException e) //异常处理
System.out.print(e.getMessage()); System.out.print( "请重新输入:" );
if (score >= 90 ) System.out.println( "优!" ); else if (score >= 80 ) System.out.println( "良!" ); else if (score >= 70 ) System.out.println( "中!" ); else if (score >= 60 ) System.out.println( "及格!" ); else System.out.println( "不及格!" );
|
运行结果:
动手动脑
[动手动脑]:编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数 代码:packagesuiji;importjava.util.Scanner;publicclassSuiji{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstub &n 查看详情
动手动脑-4(代码片段)
package动手动脑;publicclass基类public基类()System.out.println("基类Created.");public基类(Stringstring)System.out.println("基类Created.String:"+string);package动手动脑;publicclass父类extends基类public父类()super("Hell 查看详情
动手动脑
这个是让我们看下面的代码,让我们发现特别之处的动手动脑。源代码如下://MethodOverload.java//UsingoverloadedmethodspublicclassMethodOverload publicstaticvoidmain(String[]args) System.out.println("Thesquareofinteger7is" 查看详情
动手动脑
动手动脑1publicclassEnumTest publicstaticvoidmain(String[]args)//TODOAuto-generatedmethodstubSizes=Size.SMALL;Sizet=Size.LARGE;//s和t引用同一个对象?System.out.println(s= =t); ////是原始数据类型吗?System. 查看详情
动手动脑感想(代码片段)
动手动脑一:publicclasssuijishu privatestaticfinalintN=200; privatestaticfinalintLEFT=40; privatestaticfinalintRIGHT=10000; privatestaticlongx0=1L; privatelonga=1103515245L; privatelongc=12345L 查看详情
动手动脑,无法自拔
1.动手动脑 如果类提供了一个自定义的构造方法,将导致系统不再提供默认构造方法。而foo类中有一个publicfoo(intinitValue)类,导致系统不能引用默认的构造函数。2.动手动脑 实例:TextStaticInitialalizeBlock.java: classRoot{s... 查看详情
方法的动手动脑
动手动脑:(1)纯随机数发生器Modulus=231-1=int.MaxValueMultiplier=75=16807C=0当显示过231-2个数之后,才可能重复。动手动脑:编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数。代码如下:package suijishu; public&... 查看详情
动手动脑
第一个动手动脑 看老师发的文件,EnumTest.java。猜它 的运行结果。我猜s和 t肯定引用的不同对象,老师上课也讲过。falsefalsetrueSMALLMEDIUMLARGEpublicclassEnumTest{publicstaticvoidmain(String[]args){Sizes=Size.SMALL;Sizet=Size.LARGE;//s 查看详情
动手动脑
packagejxlpacakge;//信1605-320163432张运涛publicclassInitializeBlockClass{ { field=200; } publicintfield=100; publicInitializeBlockClass(intvalue)//定义有参的构造函数 { this.field=value; } publicInitializeBlockC 查看详情
动手动脑2
---恢复内容开始---1.动手实验:继承条件下的构造方法调用classGrandparentpublicGrandparent() System.out.println("GrandParentCreated."); publicGrandparent(Stringstring) System.out.println("GrandParentCreated.String:"+stri 查看详情
动手动脑
一:看代码找错误 二:java字段初始化规律package代码测试;publicclassInitializeBlockClassfield=200;publicintfield=100;publicInitializeBlockClass(intvalue)this.field=value;publicInitializeBlockClas 查看详情
课堂动手动脑课后作业
动手动脑:函数的重载package重载;publicclassjj{ publicstaticvoidmain(String[]arges) { System.out.println(o(7)); System.out.println(o(7.5)); } publicstaticinto(intx) {returnx*x;} publicstaticdoubleo(doubley) {ret 查看详情
动手动脑
课后作业01-动手动脑1、请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识结果截图:异常处理:Try{//可能发生运行错误的代码;} catch(异常类型 ... 查看详情
动手动脑03
一、以下代码输出结果是什么?publicclassInitializeBlockDemo{ /** *@paramargs */ publicstaticvoidmain(String[]args){ InitializeBlockClassobj=newInitializeBlockClass(); 查看详情
第八周动手动脑
动手动脑一:请阅读并运行AboutException.java示例AboutException.java 答:结论:异常 (Exception):发生于程序执行期间,表明出现了一个非法的运行状况。许多JDK中的方法在检测到非法情况时,都会抛出一个异常对象。例如:... 查看详情
课堂动手动脑
动手动脑1.当在捕获到异常的处理代码里加入system.exit()时,finally的代码块是不会执行的。 2.Catchwho结果: & 查看详情
动手动脑
一,代码:publicclassEnumTestprivateenumMyEnum ONE,TWO,THREE publicstaticvoidmain(String[]args) for(MyEnumvalue:MyEnum.values()) System.ou 查看详情
动手动脑
1.随机数函数函数源代码 publicclassRandomTest{ /** *@paramargs */ publicstaticvoidmain(String[]args){ //TODOAuto-generatedmethodstub java.util.Randomr=newjava.util.Random(); for(inti=0;i<5;i++){ 查看详情