Skip to content

Commit aee250d

Browse files
committed
feat: add java exception
1 parent ff999a9 commit aee250d

12 files changed

Lines changed: 251 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
- [Java 多态](https://www.wdbyte.com/java/polymorphism/)
5050
- [Java Scanner](https://www.wdbyte.com/java/scanner/)
5151
- [Java 日期时间 Date](https://www.wdbyte.com/java/date/)
52+
- [Java 异常处理](https://www.wdbyte.com/java/exception/)
5253

5354
## 🌿 SpringBoot 2.x 教程
5455

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.wdbyte.exception;
2+
3+
/**
4+
* @author niulang
5+
*/
6+
public class JavaException1 {
7+
8+
public static void main(String[] args) {
9+
String str = null;
10+
System.out.println(str.length());
11+
}
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.wdbyte.exception;
2+
3+
/**
4+
* @author niulang
5+
*/
6+
public class JavaException2 {
7+
8+
public static void main(String[] args) {
9+
String[] strArr = {"www", "wdbyte", "com"};
10+
System.out.println(strArr[0]);
11+
System.out.println(strArr[3]);
12+
}
13+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.wdbyte.exception;
2+
3+
/**
4+
* @author niulang
5+
*/
6+
public class JavaException3 {
7+
8+
public static void main(String[] args) {
9+
String[] strArr = {"www", "wdbyte", "com"};
10+
try {
11+
System.out.println(strArr[0]);
12+
System.out.println(strArr[3]);
13+
} catch (ArrayIndexOutOfBoundsException e) {
14+
System.out.println("发生数组访问越界异常,不能访问超过数组长度的元素,数组长度为:" + strArr.length);
15+
System.out.println("异常描述:" + e.getMessage());
16+
e.printStackTrace();
17+
}
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.wdbyte.exception;
2+
3+
/**
4+
* @author niulang
5+
*/
6+
public class JavaException4 {
7+
8+
public static void main(String[] args) {
9+
String[] strArr = {"www", "wdbyte", "com"};
10+
try {
11+
System.out.println(strArr[0]);
12+
System.out.println(strArr[3]);
13+
} catch (ArrayIndexOutOfBoundsException e) {
14+
//throw e;
15+
throw new RuntimeException(e);
16+
}
17+
System.out.println("执行完成。");
18+
}
19+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.wdbyte.exception;
2+
3+
import org.apache.commons.lang3.ObjectUtils.Null;
4+
5+
/**
6+
* @author niulang
7+
*/
8+
public class JavaException5 {
9+
10+
public static void main(String[] args) {
11+
String[] strArr = {"www", null, "com"};
12+
try {
13+
System.out.println(strArr[0].length());
14+
// 空指针异常
15+
System.out.println(strArr[1].length());
16+
System.out.println(strArr[3].length());
17+
} catch (ArrayIndexOutOfBoundsException e) {
18+
System.out.println("发生数组访问越界异常,不能访问超过数组长度的元素,数组长度为:" + strArr.length);
19+
} catch (NullPointerException e) {
20+
System.out.println("发现空指针异常");
21+
}
22+
System.out.println("执行完成。");
23+
24+
try {
25+
System.out.println(strArr[0].length());
26+
// 空指针异常
27+
System.out.println(strArr[1].length());
28+
// 越界异常
29+
System.out.println(strArr[3].length());
30+
} catch (Exception e) {
31+
System.out.println("发生异常:" + e.getMessage());
32+
e.printStackTrace();
33+
}
34+
}
35+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.wdbyte.exception;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Paths;
6+
7+
/**
8+
* @author niulang
9+
* @date 2023/04/27
10+
*/
11+
public class JavaException6 {
12+
13+
public static void main(String[] args) {
14+
try {
15+
Files.readAllLines(Paths.get("c:/abc.txt"));
16+
} catch (IOException e) {
17+
e.printStackTrace();
18+
}
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.wdbyte.exception;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Paths;
6+
7+
/**
8+
* @author niulang
9+
* @date 2023/04/27
10+
*/
11+
public class JavaException7 {
12+
13+
public static void main(String[] args) {
14+
try {
15+
Files.readAllLines(Paths.get("c:/abc.txt"));
16+
} catch (IOException e) {
17+
e.printStackTrace();
18+
} finally {
19+
20+
}
21+
}
22+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.wdbyte.exception;
2+
3+
/**
4+
* @author niulang
5+
*/
6+
public class JavaException8 {
7+
8+
public static void main(String[] args) {
9+
int result = 0;
10+
try {
11+
result = calc(4, 2);
12+
System.out.println("4 / 2 = " + result);
13+
} catch (MyException e) { // 不处理异常会报错
14+
e.printStackTrace();
15+
}
16+
17+
try {
18+
result = calc(4, 0);
19+
System.out.println("4 / 0 = " + result);
20+
} catch (MyException e) { // 不处理异常会报错
21+
e.printStackTrace();
22+
}
23+
}
24+
25+
public static int calc(int a, int b) throws MyException {
26+
if (b == 0) {
27+
throw new MyException("除数不能为0");
28+
}
29+
return a / b;
30+
}
31+
}
32+
33+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.wdbyte.exception;
2+
3+
public class MyException extends Exception {
4+
public MyException(String message) {
5+
super(message);
6+
}
7+
}

0 commit comments

Comments
 (0)