学习情况:略懂,不是很具体。
心得:
无。
感受:
有件事我在去年学第一门语言Python时就没想明白,为什么异常处理放在最后讲???
我是这样想的。中学时,我们有些同学会做错题本,做错题本要趁热,因为时间久了,你错了错题会没有当时的感觉。学异常处理也是相同的道理,你看异常有很多种,我们每章都会学些不同的语法,针对这些语法把相应的异常处理也学了。这样做对比与一次性学习异常处理好处在于有更多具体的例子,并且具体到每个异常,而不是一次性学习那样只有几个突出的例子。
当然,这只是想法,仔细想一下,异常处理的操作用到的不多,至少在目前这个学习阶段。
问题:
1.如何把try里面的语句扩宽到全局,举个例子,在try里面赋的值如何在try外调用。
2.我在notepad写循环时,如果用了continue那么直至循环结束,程序都会不正常运行。即使有接收的语句也会被无视。想求证一下是软件问题还是语法问题。
附程序代码
1 //Powerful-Daybreak,2017.10.22View Code
2 //没用continue
3 import java.util.*;
4 import java.io.*;
5
6 public class F
7 {
8 public static void main(String[] args) {
9 Scanner in = new Scanner(System.in);
10 System.out.println("input:");
11 String input = in.next();
12 int numer = in.nextInt();
13
14 try{
15 Students stu = new Students(input, numer);
16 System.out.println(stu.getName());
17 System.out.println(stu.getScore());
18 }
19 catch(IllegalNameException f){
20 System.out.printf("nameERORR!\n");
21 }
22 catch(IllegalScoreException e){
23 System.out.printf("scoreERORR!\n");
24 }
25
26 }
27 }
28
29 class Students{
30 private String name;
31 private int score;
32 public Students(String name, int score) throws IllegalNameException,IllegalScoreException{
33 this.name=name;
34 for (int i=0;i<10;i++)
35 {
36 String m = Integer.toString(i);
37 String h = name.substring(0,1);
38 if(h.equals(m))
39 {
40 throw new IllegalNameException("the " + name + " is not follow!");
41 }
42 }
43
44 this.score=score;
45 if(score>99 || score<0)
46 {
47 throw new IllegalScoreException("the " + score + " is not follow!");
48 }
49
50
51 }
52
53 public String getName(){
54 return name;
55 }
56
57 public int getScore(){
58 return score;
59 }
60
61 public String toString(){
62 return name + "," + score ;
63 }
64 }
65
66 class IllegalScoreException extends Exception {
67 public IllegalScoreException(String msg)
68 {
69 super(msg);
70 }
71 }
72
73 class IllegalNameException extends Exception {
74 public IllegalNameException(String msg)
75 {
76 super(msg);
77 }
78 }
79
80
81
82
83
84
85 //Powerful-Daybreak,2017.10.22
86 //用了continue
87 import java.util.*;
88
89 public class G
90 {
91 public static void main(String[] args) {
92 Scanner in = new Scanner(System.in);
93 System.out.println("input:");
94 String input = in.next();
95 int numer = in.nextInt();
96
97 for(int i = 0;i<3;i++)
98 {
99 try{
100 Students stu = new Students(input, numer);
101 System.out.println(stu.getName());
102 System.out.println(stu.getScore());
103 }
104 catch(IllegalNameException f){
105 System.out.printf("nameERORR!\n");
106 }
107 catch(IllegalScoreException e){
108 System.out.printf("scoreERORR!\n");
109 }
110 }
111 }
112 }
113
114 class Students{
115 private String name;
116 private int score;
117 public Students(String name, int score) throws IllegalNameException,IllegalScoreException{
118 this.name=name;
119 for (int i=0;i<10;i++)
120 {
121 String m = Integer.toString(i);
122 String h = name.substring(0,1);
123 if(h.equals(m))
124 {
125 throw new IllegalNameException("the " + name + " is not follow!");
126 }
127 }
128
129 this.score=score;
130 if(score>99 || score<0)
131 {
132 throw new IllegalScoreException("the " + score + " is not follow!");
133 }
134
135
136 }
137
138 public String getName(){
139 return name;
140 }
141
142 public int getScore(){
143 return score;
144 }
145
146 public String toString(){
147 return name + "," + score ;
148 }
149 }
150
151 class IllegalScoreException extends Exception {
152 public IllegalScoreException(String msg)
153 {
154 super(msg);
155 }
156 }
157
158 class IllegalNameException extends Exception {
159 public IllegalNameException(String msg)
160 {
161 super(msg);
162 }
163 }
。
图为用了continue的情况。