Java14设置异常增项信息
Java14的新特性中对于异常更精确的定位信息,如下:
package com.itranswarp.world;
public class CustomException {
public static void main(String[] args) {
PersonOne p = new PersonOne();
System.out.println(p.address.city.toLowerCase());
}
}
class PersonOne {
String[] name = new String[2];
Address address = new Address();
}
class Address {
String city;
String street;
String zipcode;
}
由上图所得,该异常是由city字段为null引起的空指针异常。定位精确。
设置增强异常信息的参数
-XX:+ShowCodeDetailsInExceptionMessages
设置路径:window>preferences>java>Installed JREs
- 选中jdk14,点击edit
- 在default VM arguments中输入:-XX:+ShowCodeDetailsInExceptionMessages
- 点击apply
- 执行测试代码看效果
查看:可以在每个Java文件上右击Run As>RunConfigurations>arguments中看到设置的参数,说明设置成功