如题所述
1ï¼å
å®ä¹ä¸ä¸ªç±»ç»§æ¿Exceptionï¼æè
å«çRuntimeExceptionçï¼ï¼
2ï¼ç¶åå个æ¹æ³å¯è½æåºè¿ä¸ªå¼å¸¸ï¼å¹¶ä¸ä»ä¹æ åµä¸ä¼æåºå¼å¸¸ï¼
3)è°ç¨è¿ä¸ªæ¹æ³ï¼åtryï¼catchè¯å¥ï¼æè·è¿ä¸ªå¼å¸¸ï¼
å°ä¾åï¼ä½ åèçä¸ï¼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class UserNotFoundException extends RuntimeException {
public UserNotFoundException() {}
public UserNotFoundException(String message) {
super(message);
}
public void f() throws UserNotFoundException {
throw new UserNotFoundException("ç¨æ·åä¸å¯¹");
}
}
public class Exception {
public static void main(String args[]) {
try {
new UserNotFoundException().f();
} catch (UserNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
2ï¼ç¶åå个æ¹æ³å¯è½æåºè¿ä¸ªå¼å¸¸ï¼å¹¶ä¸ä»ä¹æ åµä¸ä¼æåºå¼å¸¸ï¼
3)è°ç¨è¿ä¸ªæ¹æ³ï¼åtryï¼catchè¯å¥ï¼æè·è¿ä¸ªå¼å¸¸ï¼
å°ä¾åï¼ä½ åèçä¸ï¼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class UserNotFoundException extends RuntimeException {
public UserNotFoundException() {}
public UserNotFoundException(String message) {
super(message);
}
public void f() throws UserNotFoundException {
throw new UserNotFoundException("ç¨æ·åä¸å¯¹");
}
}
public class Exception {
public static void main(String args[]) {
try {
new UserNotFoundException().f();
} catch (UserNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
温馨提示:答案为网友推荐,仅供参考