代码 BUG 处理
1. 代码问题 TradeInfo空指针问题
sonarQube 扫描出 可能会出现空指针问题
– 代码复现 DY001Service
// sonarQube 源代码 扫描 可能会出现空指针异常
TradeInfo msfPrintPackMess = null; by yangkai
if (msfPrintPackMess.getErrorList().size() == 0) {
// ingore;
}
– 处理方法
修改后 TradeInfo msfPrintPackMess = (TradeInfo) tradeInfo;
2. 代码问题 finally中包含有 return
– 代码复现 YBT001Service
try {
return xxx;
} catch(Exception e) {
return xxx;
} finally {
return xxx;
}
代码中 finally 不要包含 return;
– 例子如下
/**
* @author : DJ032915
* @description :
* @program :
* @date : 2023-01-16
*/
public class TryCatchTest {
public static void main(String[] args) {
// System.out.println("return的返回值:" + test());
System.out.println("包含异常return的返回值:" + testWithException());
// System.out.println("return的返回值:" + testWithObject().age); // 测试返回值类型是对象时
}
// finally是在return后面的表达式运算之后执行的,此时并没有返回运算之后的值
//,而是把值保存起来,不管finally对该值做任何的改变,返回的值都不会改变,依然返回保存起来的值。
//也就是说方法的返回值是在finally运算之前就确定了的。
static int test() {
int x = 1;
try {
return x++;
} catch(Exception e){
}finally {
System.out.println("finally:" + x);//finally =2, 说明 try中return 后的表达式x++执行了
++x;
System.out.println("++x:" + x);//x=3
return x; //执行到这里,程序便结束,此时返回的值为3,即test()执行结果为3 。说明try中return不会被执行
//编译器把finally中的return语句标识为一个warning. 警告为'return' inside 'finally' block
//Inspection info: Reports return statements inside of finally blocks.
//While occasionally intended, such return statements may mask thrown exceptions and complicate debugging.
// 翻译为中文
// 'finally'块中的'return'
// 检查信息:报告finally块内部的返回语句。
// 虽然偶尔是有意的,但这样的返回语句可能会掩盖抛出的异常并使调试复杂化。
// 所以 finally代码中最好不要包含return.
}
}
static int testWithException(){
Integer x = null;
try {
x.intValue(); // 造个空指针异常
return x++;
} catch(Exception e){
e.printStackTrace();
System.out.println("catch:" + x);// x=null
x = 1;
return x; // 返回1
// return ++x; // 返回2
}finally {
x = 1;
System.out.println("finally:" + x);
++x;
System.out.println("++x:" + x);
// finally代码中最好不要包含return,程序会提前退出,
// 也就是说返回的值不是try或catch中的值
return x;
}
}
static Num testWithObject() {
Num num = new Num();
try {
return num;
} catch(Exception e){
}finally {
num.age++; // 改变了引用对象的值
System.out.println("finally:" + num.age); //num.age=1
}
return num;
}
static class Num{
public int age;
}
}
3. 代码问题 方法返回null 是否会爆出空指针异常 答案:不会
– 代码复现
/**
* @author : DJ032915
* @description :
* @program :
* @date : 2023-01-16
*/
public class ReturnNull {
public static void main(String[] args) {
String value = null;
try {
value = (String) getNull();
System.out.println(value);
if (isBlank(value)) {
System.out.println("true not have bugs");
}
}catch (Exception e){
System.out.println(e.getMessage());
}
}
// null 方法
public static Object getNull(){
return null;
}
// isBlank 是 封装的判空方法
public static boolean isBlank(CharSequence cs) {
int strLen;
if (cs == null || (strLen = cs.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (Character.isWhitespace(cs.charAt(i)) == false) {
return false;
}
}
return true;
}
}
4. 代码问题 InterruptedException异常
– 问题复现
private void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
throw new RuntimeException(THREAD_NAME + "收到中断异常!", e);
}
}
–问题解决
private void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(THREAD_NAME + "收到中断异常!", e);
}
}
5. 问题代码 流对象 空指针异常
– 代码复现
import java.io.IOException;
import java.io.InputStream;
/**
* @author : DJ032915
* @description :
* @program :
* @date : 2023-01-16
*/
public class StreamClose {
public static void main(String[] args) {
InputStream fin = null;
try {
fin.close();
} catch (IOException e) {
System.out.println("关闭流出错:" + e);
}
}
}
– 问题解决
import java.io.IOException;
import java.io.InputStream;
/**
* @author : DJ032915
* @description :
* @program :
* @date : 2023-01-16
*/
public class StreamClose {
public static void main(String[] args) {
InputStream fin = null;
if (fin!=null) {
try {
fin.close();
} catch (IOException e) {
System.out.println("关闭流出错:" + e);
}
}
}
}