如何在空指针异常中发送这些记录器。我怎样才能在这里进行异常处理?另外,如何在末尾打印一个类似“status”的布尔变量,说代码中遇到异常?
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之前。如果是,则转到成功的代码块,否则抛出错误。为此,我使用布尔变量“Status”。但我不确定我是否采取了正确的方式。 此外,我试图看看empId是否存在,如果它是null然后抛出NPE。同样,“status”可用于返回异常。
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.");
}
答案 0 :(得分:1)
您想要使用这种方法:
throw new NullPointerException("myMessage");
您可以将所有语句包装在try catch块中。该块应该能够捕获任何内部抛出的异常。
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
}
关于你的第二个问题。我看到了几种不同的方法来处理它。
在下面的块中,您可以抛出异常:
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!");
}
您不能将布尔值直接传递给Exception构造函数。相反,你必须获得该布尔值的字符串表示。将String.valueOf(状态)。