Skip to content

Commit 4bd4f9b

Browse files
committed
add post
1 parent 45a5960 commit 4bd4f9b

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

source/_posts/.idea/workspace.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/_posts/Java14新特性:Switch表达式.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ cover: true
99

1010
---
1111

12-
![](http://q6pznk9ej.bkt.clouddn.com/img%20%288%29.jpeg)
12+
![](http://q6pznk9ej.bkt.clouddn.com/java.jpg)
1313
<!-- more -->
1414
>Java 14正式发布switch表达式特性。在之前的两个 Java 版本Java12,Java13,switch特性只是预览版。
1515
新的switch表达式有助于避免一些bug,因为它的表达和组合方式更容易编写。
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
title: Java14新特性:增强NPE NullPointerException 错误推断
2+
tags:
3+
- java14
4+
categories:
5+
- Dev
6+
- Java
7+
date: 2020-03-19 08:41:00
8+
cover: true
9+
10+
---
11+
12+
![](http://q6pznk9ej.bkt.clouddn.com/java.jpg)
13+
<!-- more -->
14+
>改进 NullPointerExceptions,通过准确描述哪些变量为 null 来提高 JVM 生成的异常的可用性。该提案的作者希望为开发人员和支持人员提供有关程序为何异常终止的有用信息,并通过更清楚地将动态异常与静态程序代码相关联来提高对程序的理解。
15+
16+
```
17+
String name = user.getLocation().getCity().getName();
18+
```
19+
在Java 14之前,你可能会得到如下的错误:
20+
```
21+
Exception in thread "main" java.lang.NullPointerExceptionat NullPointerExample.main(NullPointerExample.java:2)
22+
```
23+
不幸的是,如果在第2行是一个包含了多个方法调用的赋值语句(如getLocation()和getCity()),那么任何一个都可能会返回null。实际上,变量user也可能是null。因此,无法判断是谁导致了NullPointerException。
24+
25+
在Java 14中,新的JVM特性可以显示更详细的诊断信息:
26+
```
27+
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Location.getCity()"
28+
because the return value of "User.getLocation()" is null at NullPointerExample.main(NullPointerExample.java:2)
29+
```
30+
31+
该消息包含两个明确的组成部分:
32+
* 后果:`Location.getCity()`无法被调用
33+
* 原因:`User.getLocation()`的返回值为null

0 commit comments

Comments
 (0)