java - 如何在抛出错误的同时返回一个空列表?
问题描述
我现在正在做作业,如果该字段为空,我被要求返回一个空列表并打印一条错误消息。
到目前为止,这是我的代码,但是抛出错误或返回空列表(如果我更改顺序,则为后一个)是无法访问的。我该怎么做?
// the method is not void it returns a list
if (fileContent.equals(null)) {
return Collections.emptyList();
throw new Error("ERROR: No content loaded before parsing.");
} else {
// return another list ;
}
这是我得到的错误:
org.junit.ComparisonFailure: Console output not as expected in parseFileContent.
Expected :ERROR: No content loaded before parsing.
Actual :ERROR: executing parseFileContent for console output check:java.lang.NullPointerException
谢谢!
解决方案
我猜你不需要抛出错误,而只需要打印它。在这种情况下,请执行以下操作:
if(fileContent.equals(null)) {
// If you need to print the error:
System.err.println("ERROR: No content loaded before parsing.");
return Collections.EMPTY_LIST;
} else {
// return anotherList
}
推荐阅读
- python - 在 Python 中加载 Yaml 文件而不转换为 dict
- spring - Spring MVC-Activation account by token-confirm account方法返回404错误
- prolog - Prolog 部分匹配
- amazon-sns - Amazon SNS 短信
- spring-boot - 后端springboot中的jwt验证
- php - Braintree授权验证和存储信用卡php
- java - Collections.sort 用于多个条件
- python - Django - Postgresql 数据库恢复 - IntegrityError
- jquery - 使用 Jquery 在输入类型号上触发 onChange 事件
- reactjs - 未找到模块:无法解析“C:\Users\test\counter-app\src”中的“bootstrap/dist/css/bootstrap.css”