Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_java
提交
432c302c
S
skill_tree_java
项目概览
CSDN 技术社区
/
skill_tree_java
通知
43
Star
8
Fork
4
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
4
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_java
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
4
Issue
4
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
432c302c
编写于
12月 24, 2021
作者:
ccat
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'dev/exception' into 'master'
dev(hansbug): add problems in exception part See merge request
!2
上级
e19310cf
173c3115
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
446 addition
and
1 deletion
+446
-1
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/checked_exception.json
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/checked_exception.json
+8
-0
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/checked_exception.md
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/checked_exception.md
+247
-0
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/config.json
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/config.json
+4
-1
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/runtime_exception.json
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/runtime_exception.json
+8
-0
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/runtime_exception.md
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/runtime_exception.md
+179
-0
未找到文件。
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/checked_exception.json
0 → 100644
浏览文件 @
432c302c
{
"type"
:
"code_options"
,
"author"
:
"HansBug"
,
"source"
:
"checked_exception.md"
,
"notebook_enable"
:
false
,
"exercise_id"
:
"302ed00decc24b4ba435aab028582ac4"
}
\ No newline at end of file
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/checked_exception.md
0 → 100644
浏览文件 @
432c302c
# 自定义可查异常(Checked Exception)
现有如下的学生考试成绩查询程序,会根据给定的字符串格式
`studentID`
进行学生分数查询,如果该学生存在,则返回其分数(确保为非负整数);如果该学生不存在,则返回
`-1`
,如下所示
```
java
public
class
TestMain
{
private
static
boolean
containsStudent
(
String
studentID
)
{
// Return true when the given `studentID` represents an existing student.
}
private
static
int
queryScore
(
String
studentID
)
{
// Get and then return score of the student
// whose ID is `StudentID` from the database.
}
private
static
int
getScoreByStudentID
(
String
studentID
)
{
if
(
containsStudent
(
studentID
))
{
return
queryScore
(
studentID
);
}
else
{
return
-
1
;
}
}
public
static
void
main
(
String
[]
args
)
{
String
studentID
=
"ID20211224"
;
int
score
=
getScoreByStudentID
(
studentID
);
if
(
score
>=
0
)
{
System
.
out
.
printf
(
"Student ID : %s%n"
,
studentID
);
System
.
out
.
printf
(
"Score : %d%n"
,
score
);
}
else
{
System
.
out
.
printf
(
"Student ID %s not found!%n"
,
studentID
);
}
}
}
```
请改写上述代码,对于学生不存在的情况,
**使用可查异常(Checked Exception)对潜在的异常情况进行妥善处理**
而不是返回
`-1`
。
> 可查异常(Checked Exception)是编译器在编译时会进行检查,也称为编译时异常(Compile Time Exception)。 这些异常往往具备可预见性,且不能简单地被忽略,编程者应当对其进行妥善处理。
因此,下列修改方式正确的是:
## 答案
```
java
public
class
TestMain
{
private
static
boolean
containsStudent
(
String
studentID
)
{
// Return true when the given `studentID` represents an existing student.
}
private
static
int
queryScore
(
String
studentID
)
{
// Get and then return score of the student
// whose ID is `StudentID` from the database.
}
public
static
class
StudentNotExistException
extends
Exception
{
public
StudentNotExistException
(
String
studentID
)
{
super
(
String
.
format
(
"Student ID %s not found."
,
studentID
));
}
}
private
static
int
getScoreByStudentID
(
String
studentID
)
throws
StudentNotExistException
{
if
(
containsStudent
(
studentID
))
{
return
queryScore
(
studentID
);
}
else
{
throw
new
StudentNotExistException
(
studentID
);
}
}
public
static
void
main
(
String
[]
args
)
{
String
studentID
=
"ID20211224"
;
try
{
int
score
=
getScoreByStudentID
(
studentID
);
System
.
out
.
printf
(
"Student ID : %s%n"
,
studentID
);
System
.
out
.
printf
(
"Score : %d%n"
,
score
);
}
catch
(
StudentNotExistException
err
)
{
System
.
out
.
printf
(
"Student ID %s not found!%n"
,
studentID
);
}
}
}
```
## 选项
### A
```
java
public
class
TestMain
{
private
static
boolean
containsStudent
(
String
studentID
)
{
// Return true when the given `studentID` represents an existing student.
}
private
static
int
queryScore
(
String
studentID
)
{
// Get and then return score of the student
// whose ID is `StudentID` from the database.
}
public
static
class
StudentNotExistException
extends
RuntimeException
{
public
StudentNotExistException
(
String
studentID
)
{
super
(
String
.
format
(
"Student ID %s not found."
,
studentID
));
}
}
private
static
int
getScoreByStudentID
(
String
studentID
)
throws
StudentNotExistException
{
if
(
containsStudent
(
studentID
))
{
return
queryScore
(
studentID
);
}
else
{
throw
new
StudentNotExistException
(
studentID
);
}
}
public
static
void
main
(
String
[]
args
)
{
String
studentID
=
"ID20211224"
;
try
{
int
score
=
getScoreByStudentID
(
studentID
);
System
.
out
.
printf
(
"Student ID : %s%n"
,
studentID
);
System
.
out
.
printf
(
"Score : %d%n"
,
score
);
}
catch
(
StudentNotExistException
err
)
{
System
.
out
.
printf
(
"Student ID %s not found!%n"
,
studentID
);
}
}
}
```
### B
```
java
public
class
TestMain
{
private
static
boolean
containsStudent
(
String
studentID
)
{
// Return true when the given `studentID` represents an existing student.
}
private
static
int
queryScore
(
String
studentID
)
{
// Get and then return score of the student
// whose ID is `StudentID` from the database.
}
public
static
class
StudentNotExistException
extends
RuntimeException
{
public
StudentNotExistException
(
String
studentID
)
{
super
(
String
.
format
(
"Student ID %s not found."
,
studentID
));
}
}
private
static
int
getScoreByStudentID
(
String
studentID
)
{
if
(
containsStudent
(
studentID
))
{
return
queryScore
(
studentID
);
}
else
{
throw
new
StudentNotExistException
(
studentID
);
}
}
public
static
void
main
(
String
[]
args
)
{
String
studentID
=
"ID20211224"
;
int
score
=
getScoreByStudentID
(
studentID
);
System
.
out
.
printf
(
"Student ID : %s%n"
,
studentID
);
System
.
out
.
printf
(
"Score : %d%n"
,
score
);
}
}
```
### C
```
java
public
class
TestMain
{
private
static
boolean
containsStudent
(
String
studentID
)
{
// Return true when the given `studentID` represents an existing student.
}
private
static
int
queryScore
(
String
studentID
)
{
// Get and then return score of the student
// whose ID is `StudentID` from the database.
}
public
static
class
StudentNotExistException
extends
Error
{
public
StudentNotExistException
(
String
studentID
)
{
super
(
String
.
format
(
"Student ID %s not found."
,
studentID
));
}
}
private
static
int
getScoreByStudentID
(
String
studentID
)
throws
StudentNotExistException
{
if
(
containsStudent
(
studentID
))
{
return
queryScore
(
studentID
);
}
else
{
throw
new
StudentNotExistException
(
studentID
);
}
}
public
static
void
main
(
String
[]
args
)
{
String
studentID
=
"ID20211224"
;
try
{
int
score
=
getScoreByStudentID
(
studentID
);
System
.
out
.
printf
(
"Student ID : %s%n"
,
studentID
);
System
.
out
.
printf
(
"Score : %d%n"
,
score
);
}
catch
(
StudentNotExistException
err
)
{
System
.
out
.
printf
(
"Student ID %s not found!%n"
,
studentID
);
}
}
}
```
### D
```
java
public
class
TestMain
{
private
static
boolean
containsStudent
(
String
studentID
)
{
// Return true when the given `studentID` represents an existing student.
}
private
static
int
queryScore
(
String
studentID
)
{
// Get and then return score of the student
// whose ID is `StudentID` from the database.
}
public
static
class
StudentNotExistException
extends
Exception
{
public
StudentNotExistException
(
String
studentID
)
{
super
(
String
.
format
(
"Student ID %s not found."
,
studentID
));
}
}
private
static
int
getScoreByStudentID
(
String
studentID
)
throws
StudentNotExistException
{
if
(
containsStudent
(
studentID
))
{
return
queryScore
(
studentID
);
}
else
{
throw
new
StudentNotExistException
(
studentID
);
}
}
public
static
void
main
(
String
[]
args
)
{
String
studentID
=
"ID20211224"
;
try
{
int
score
=
getScoreByStudentID
(
studentID
);
System
.
out
.
printf
(
"Student ID : %s%n"
,
studentID
);
System
.
out
.
printf
(
"Score : %d%n"
,
score
);
}
except
(
StudentNotExistException
err
)
{
System
.
out
.
printf
(
"Student ID %s not found!%n"
,
studentID
);
}
}
}
```
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/config.json
浏览文件 @
432c302c
...
...
@@ -223,7 +223,9 @@
],
"export"
:
[
"exception.json"
,
"using.json"
"using.json"
,
"runtime_exception.json"
,
"checked_exception.json"
],
"title"
:
"通过异常处理错误"
}
\ No newline at end of file
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/runtime_exception.json
0 → 100644
浏览文件 @
432c302c
{
"type"
:
"code_options"
,
"author"
:
"HansBug"
,
"source"
:
"runtime_exception.md"
,
"notebook_enable"
:
false
,
"exercise_id"
:
"61a61ce93dca4b2e9049f5318e4d2a21"
}
\ No newline at end of file
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/runtime_exception.md
0 → 100644
浏览文件 @
432c302c
# 自定义运行时异常(RuntimeException)
现有如下的海伦公式计算函数,根据三角形的三条边长
`a`
、
`b`
和
`c`
(均确保大于0)计算三角形的面积。当三角形本身非法时返回负数,否则返回面积,如下所示
```
java
public
class
TestMain
{
private
static
float
getArea
(
float
a
,
float
b
,
float
c
)
{
if
((
a
+
b
<
c
)
||
(
a
+
c
<
b
)
||
(
b
+
c
<
a
))
{
return
-
1
;
// 非法三角形
}
else
{
float
p
=
(
a
+
b
+
c
)
/
2
;
return
(
float
)
Math
.
sqrt
((
p
-
a
)
*
(
p
-
b
)
*
(
p
-
c
)
*
p
);
// 海伦公式
}
}
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
TestMain
.
getArea
(
3
,
4
,
5
));
}
}
```
请改写上述代码,对于三角形非法的情况,
**仅在运行抛出不可查异常(Unchecked Exception)**
而不是返回
`-1`
。
> 不可查异常(Unchecked Exception)指在运行时发生的异常。这些也被称为运行时异常(Runtime Exception)。其中包括程序逻辑错误或API使用不当等,例如ArrayIndexOutOfBoundsException、IllegalStateException与NullPointerException等,此类异常将在编译时忽略,仅在运行时处理。
因此,下列修改方式正确的是:
## 答案
```
java
public
class
TestMain
{
public
static
class
InvalidTriangleException
extends
RuntimeException
{
public
InvalidTriangleException
(
float
a
,
float
b
,
float
c
)
{
super
(
String
.
format
(
"Invalid triangle - (%.3f, %.3f, %.3f)."
,
a
,
b
,
c
));
}
}
private
static
float
getArea
(
float
a
,
float
b
,
float
c
)
{
if
((
a
+
b
<
c
)
||
(
a
+
c
<
b
)
||
(
b
+
c
<
a
))
{
throw
new
InvalidTriangleException
(
a
,
b
,
c
);
}
else
{
float
p
=
(
a
+
b
+
c
)
/
2
;
return
(
float
)
Math
.
sqrt
((
p
-
a
)
*
(
p
-
b
)
*
(
p
-
c
)
*
p
);
}
}
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
TestMain
.
getArea
(
3
,
4
,
5
));
}
}
```
## 选项
### A
```
java
public
class
TestMain
{
public
class
InvalidTriangleException
extends
RuntimeException
{
public
InvalidTriangleException
(
float
a
,
float
b
,
float
c
)
{
super
(
String
.
format
(
"Invalid triangle - (%.3f, %.3f, %.3f)."
,
a
,
b
,
c
));
}
}
private
float
getArea
(
float
a
,
float
b
,
float
c
)
{
if
((
a
+
b
<
c
)
||
(
a
+
c
<
b
)
||
(
b
+
c
<
a
))
{
throw
new
InvalidTriangleException
(
a
,
b
,
c
);
}
else
{
float
p
=
(
a
+
b
+
c
)
/
2
;
return
(
float
)
Math
.
sqrt
((
p
-
a
)
*
(
p
-
b
)
*
(
p
-
c
)
*
p
);
}
}
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
TestMain
.
getArea
(
3
,
4
,
5
));
}
}
```
### B
```
java
public
class
TestMain
{
public
static
class
InvalidTriangleException
extends
Exception
{
public
InvalidTriangleException
(
float
a
,
float
b
,
float
c
)
{
super
(
String
.
format
(
"Invalid triangle - (%.3f, %.3f, %.3f)."
,
a
,
b
,
c
));
}
}
private
static
float
getArea
(
float
a
,
float
b
,
float
c
)
{
if
((
a
+
b
<
c
)
||
(
a
+
c
<
b
)
||
(
b
+
c
<
a
))
{
throw
new
InvalidTriangleException
(
a
,
b
,
c
);
}
else
{
float
p
=
(
a
+
b
+
c
)
/
2
;
return
(
float
)
Math
.
sqrt
((
p
-
a
)
*
(
p
-
b
)
*
(
p
-
c
)
*
p
);
}
}
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
TestMain
.
getArea
(
3
,
4
,
5
));
}
}
```
### C
```
java
public
class
TestMain
{
public
static
class
InvalidTriangleException
extends
Exception
{
public
InvalidTriangleException
(
float
a
,
float
b
,
float
c
)
{
super
(
String
.
format
(
"Invalid triangle - (%.3f, %.3f, %.3f)."
,
a
,
b
,
c
));
}
}
private
static
float
getArea
(
float
a
,
float
b
,
float
c
)
throws
InvalidTriangleException
{
if
((
a
+
b
<
c
)
||
(
a
+
c
<
b
)
||
(
b
+
c
<
a
))
{
throw
new
InvalidTriangleException
(
a
,
b
,
c
);
}
else
{
float
p
=
(
a
+
b
+
c
)
/
2
;
return
(
float
)
Math
.
sqrt
((
p
-
a
)
*
(
p
-
b
)
*
(
p
-
c
)
*
p
);
}
}
public
static
void
main
(
String
[]
args
)
throws
InvalidTriangleException
{
System
.
out
.
println
(
TestMain
.
getArea
(
3
,
4
,
5
));
}
}
```
### D
```
java
public
class
TestMain
{
public
static
class
InvalidTriangleException
extends
Error
{
public
InvalidTriangleException
(
float
a
,
float
b
,
float
c
)
{
super
(
String
.
format
(
"Invalid triangle - (%.3f, %.3f, %.3f)."
,
a
,
b
,
c
));
}
}
private
static
float
getArea
(
float
a
,
float
b
,
float
c
)
{
if
((
a
+
b
<
c
)
||
(
a
+
c
<
b
)
||
(
b
+
c
<
a
))
{
throw
new
InvalidTriangleException
(
a
,
b
,
c
);
}
else
{
float
p
=
(
a
+
b
+
c
)
/
2
;
return
(
float
)
Math
.
sqrt
((
p
-
a
)
*
(
p
-
b
)
*
(
p
-
c
)
*
p
);
}
}
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
TestMain
.
getArea
(
3
,
4
,
5
));
}
}
```
### E
```
java
public
class
TestMain
{
public
static
class
InvalidTriangleException
extends
RuntimeException
{
public
InvalidTriangleException
(
float
a
,
float
b
,
float
c
)
{
super
(
String
.
format
(
"Invalid triangle - (%.3f, %.3f, %.3f)."
,
a
,
b
,
c
));
}
}
private
static
float
getArea
(
float
a
,
float
b
,
float
c
)
throws
InvalidTriangleException
{
if
((
a
+
b
<
c
)
||
(
a
+
c
<
b
)
||
(
b
+
c
<
a
))
{
throw
new
InvalidTriangleException
(
a
,
b
,
c
);
}
else
{
float
p
=
(
a
+
b
+
c
)
/
2
;
return
(
float
)
Math
.
sqrt
((
p
-
a
)
*
(
p
-
b
)
*
(
p
-
c
)
*
p
);
}
}
public
static
void
main
(
String
[]
args
)
throws
InvalidTriangleException
{
System
.
out
.
println
(
TestMain
.
getArea
(
3
,
4
,
5
));
}
}
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录