gpt4 book ai didi

java - 如何将记录器作为空指针异常发送

转载 作者:行者123 更新时间:2023-12-01 11:21:54 25 4
gpt4 key购买 nike

如何在空指针异常中发送这些记录器。这里如何进行异常处理呢?另外,如何使用像“status”这样的 boolean 变量在最后打印,表示代码中遇到了异常?

String strSchActStartTime = inData.getTransactionDetails().get("Schedule Actual Start Time");
if (strSchActStartTime == null) {
logger.debug("Schedule Actual Start Time is null "); //need to send this as an EXCEPTION in the response.
}
String strSchActEndTime = inData.getTransactionDetails().get("Schedule Actual End Time");
if (strSchActEndTime == null) {
logger.debug("Schedule Actual End Time is null "); //need to send this as an EXCEPTION in the response.
}
String breakStr = inData.getTransactionDetails().get("Break String");
String newValue = "\"EMPSKD_ACT_START_TIME" + slot + "=" + strSchActStartTime.trim() + "\"";
newValue = newValue + ",\"EMPSKD_ACT_END_TIME" + slot + "=" + strSchActEndTime.trim() + "\"";
if (breakStr != null) {
newValue = newValue + ",\"EMPSKD_BRKS=" + breakStr.trim() + "\"";
}
ovr.setOvrNewValue(newValue);
logger.debug("Schedule New Value : " + newValue);
oa.insert(ovr);

我的代码的第二部分如下。在这里,我尝试做一些事情。我想检查 ovrStartDate 是否在 otlGoLiveDate 之前以及 getPaygrpHandsOffDate 之前。如果是,则转到成功的代码块,否则抛出错误。为此,我使用 boolean 变量“Status”。但我不确定我的做法是否正确。另外,我试图查看 empId 是否存在,如果为 null 则抛出 NPE。这里“状态”再次可用于返回异常。

Date otlGoLiveDate = AWAOTLHelper.getEmpGoLiveDate(ovr.getEmpId(), conn);           
logger.debug("otlGoLiveDate is: " + otlGoLiveDate.toString());
if(otlGoLiveDate.after(ovr.getOvrStartDate())){
ovr.setOvrStatus(OverrideData.ERROR);
status = false;
ovr.setOvrMessage("Cannot enter overrides before go live date.");
}
/*if((!ovr.getWbuName().equals("HR_REFRESH")) && otlGoLiveDate.after(ovr.getOvrStartDate())){
ovr.setOvrStatus(OverrideData.ERROR);
ovr.setOvrMessage("Cannot enter overrides before go live date.");
}*/
EmployeeAccess ea = new EmployeeAccess(conn, CodeMapper.createCodeMapper(conn));
EmployeeData ed = ea.load(empId, ovrStartDate);
int payGrpId = ed.getPaygrpId();
PayGroupAccess pga = new PayGroupAccess(conn);
PayGroupData pgd = pga.load(payGrpId);
pgd.getPaygrpHandsOffDate();
logger.debug("PaygrpHandsOffDate : " + pgd.getPaygrpHandsOffDate());
if (ovrStartDate.before(pgd.getPaygrpHandsOffDate())) {
status = false;
ovr.setOvrMessage("Cannot enter overrides before Hands Off Date.");
}

最佳答案

您想使用这种类型的方法:

throw new NullPointerException("myMessage");

您可以将所有语句包装在 try catch block 中。该 block 应该能够捕获任何内部引发的异常。

try{
String strSchActStartTime = inData.getTransactionDetails().get("Schedule Actual Start Time");
if (strSchActStartTime == null) {
//logger.debug("Schedule Actual Start Time is null "); //need to send this as an EXCEPTION in the response.
throw new NullPointerException("Schedule Actual Start Time is null");
}
String strSchActEndTime = inData.getTransactionDetails().get("Schedule Actual End Time");
if (strSchActEndTime == null) {
logger.debug("Schedule Actual End Time is null "); //need to send this as an EXCEPTION in the response.
}
String breakStr = inData.getTransactionDetails().get("Break String");
String newValue = "\"EMPSKD_ACT_START_TIME" + slot + "=" + strSchActStartTime.trim() + "\"";
newValue = newValue + ",\"EMPSKD_ACT_END_TIME" + slot + "=" + strSchActEndTime.trim() + "\"";
if (breakStr != null) {
newValue = newValue + ",\"EMPSKD_BRKS=" + breakStr.trim() + "\"";
}
ovr.setOvrNewValue(newValue);
logger.debug("Schedule New Value : " + newValue);
oa.insert(ovr);
} catch (NullPointerException npe){
//Code to handle the error
}

关于你的第二个问题。我看到了几种不同的处理方法。

在下面的 block 中,您可以抛出异常:

if(otlGoLiveDate.after(ovr.getOvrStartDate())){
ovr.setOvrStatus(OverrideData.ERROR);
status = false;
ovr.setOvrMessage("Cannot enter overrides before go live date.");
throw new Exception("an error occurred");
}

或者在您可以放置​​的所有内容的末尾:

if(!status){
throw new Exception("An error has occurred!");
}

您不能将 boolean 值直接传递到异常构造函数中。相反,您必须获取该 boolean 值的字符串表示形式。 String.valueOf(状态)。

关于java - 如何将记录器作为空指针异常发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31128463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com