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
/
exceptions
/
Demo1.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
43 lines (41 loc) · 1.54 KB
master
Breadcrumbs
alibabaStudy
/
src
/
main
/
java
/
com
/
exceptions
/
Demo1.java
Copy path
Top
File metadata and controls
Code
Blame
43 lines (41 loc) · 1.54 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
package
com
.
exceptions
;
/**
* @author Liuys
* @version V1.0
* @Package com.exceptions
* @date 2020/1/15 13:39
*/
public
class
Demo1
{
/***
* 对给定的数组通过给定的角标获取元素
* @param arr
* @param index
* @return
*/
int
getElements
(
int
[]
arr
,
int
index
){
/***
* jvm出了问题,自己打包对象并抛出。
* 但是它所提供的信息不够详细,想要更清晰,需要自己写
*/
if
(
arr
==
null
){
throw
new
NullPointerException
(
"arr指定的数组不存在"
);
}
if
(
index
<
0
||
index
>=
arr
.
length
){
//该条件如果满足,功能已经不无法继续运算,这时就必须结束功能。并将问题告知给调用者,这是就需要通过异常来解决。
//1.创建一个异常对象 。封装一些提示信息(自定义)
//2.需要将这个对象告知给调用者,通过throw异常对象
//ArrayIndexOutOfBoundsException有多个构造函数
//throw new ArrayIndexOutOfBoundsException(index);
//throw new ArrayIndexOutOfBoundsException(index);
//throw new ArrayIndexOutOfBoundsException(index + "");
throw
new
ArrayIndexOutOfBoundsException
(
"错误的角标"
+
index
+
"不在数组中!"
);
}
int
element
=
arr
[
index
];
return
element
;
}
public
static
void
main
(
String
[]
args
){
Demo1
demo1
=
new
Demo1
();
int
[]
arr
= {
2
,
3
,
4
};
demo1
.
getElements
(
arr
,
4
);
}
}
You can’t perform that action at this time.