Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
java-liu
alibabaStudy
Repository navigation
Code
Issues
Pull requests
9
(9)
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
alibabaStudy
/
src
/
main
/
java
/
com
/
entity
/
Person.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
47 lines (42 loc) · 1.9 KB
master
Breadcrumbs
alibabaStudy
/
src
/
main
/
java
/
com
/
entity
/
Person.java
Copy path
Top
File metadata and controls
Code
Blame
47 lines (42 loc) · 1.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
com
.
entity
;
/**
* @author Liuys
* @version V1.0
* @Package com.entity
* @date 2020/1/15 14:03
*/
public
class
Person
{
private
String
name
;
private
int
age
;
/***
* 构造函数到底抛出这个IllegalArgumentException 是继承Exception呢?还是继承RuntimeException呢?
* 继承Exception,必须要throws声明,一声明就告知调用者进行捕获,一旦问题处理了调用者的程序会继续执行。
* 但是如果使用到了Person对象的数据,会导致都失败的。
* 继承RuntimeException,不需要throws声明的,这是调用是不可能编写捕获代码的,因为调用根本就不知道有问题。
* 一旦发生IllegalArgumentException,调用者程序会停掉,并有jvm将信息显示到屏幕,让调用者看到问题,然后修正代码。
* @param name
* @param age
*/
public
Person
(
String
name
,
int
age
){
//加入逻辑判断
// (不过一般不这么写)
if
(
age
<
0
||
age
>
200
){
/***
* 这样做虽然可以编译并运行看到消息,但是问题却没有发生,程序还在继续执行。并打印p对象。
* 这是不合理的,人对象初始化过程已经出了问题,为何要会对人对象操作
* 所以应该将问题暴露出来,让使用该程序的调用者知道,
* 所以要使用异常来解决
*/
/*System.out.println("年龄数值错误");
return;*/
// 抛出(运行时)异常,但是不用声明,不需要调用者处理。就需要一旦问题发生让调用者停止,让其修改代码。
throw
new
IllegalArgumentException
(
"年龄数值错误"
);
}
this
.
name
=
name
;
this
.
age
=
age
;
}
@
Override
public
String
toString
(){
return
"Person[name="
+
name
+
",age="
+
age
+
"]"
;
}
}
You can’t perform that action at this time.