Exception.getMessage() is null

记录一个今天遇到的小问题,Exception.getMessage() null,和网上这个例子比较相似

For example

try
{
    if (stud.getCall() != null)
        acc.Call = stud.getCall().toString();
    else
        throw new Exception("Data is null");
}
catch (Exception e)
{
    logger.error("Some Error" + e.getMessage());
    throw new Exception("Please check the Manatadatory Field is Missing" + e.getMessage());
}

最后输出结果为:

Some Error null

 

Answer :  You are catching an exception other than the one that your code is explicitly creating and throwing (1). The exception that you are catching doesn't have a message. You need to log the entire exception, not just the exception's message. (Among other things, that would tell you what the caught exception's actual class is and where the exception was created/thrown.)

Based on the fact that the exception doesn't have a message, I'd guess that it is an NPE caused by stud or acc being null, or by stud.getCall() returning null ... or something like that. A NullPointerException generated natively (i.e. by the JVM) has a null message(2).

 

Throwing java.lang.Exception is Bad Practice

Your problem illustrates why it is generally speaking a bad idea to create/throw Exception

When you throw Exception it becomes next to impossible to discriminate between it and other (unexpected) exceptions in a catch clause. And that is what has happened here: you've caught the wrong exception.

You should pick a more specific exception, and if no appropriate one exists, implement one of your own.


1 - You could also use e.printStackTrace(), a logger call, or a debugger to find out which exception you have actually caught.

2 - This is not true on Android. There, an NPE has a helpful message that gives contextual information.

给这个方法添加单元测试: public TdsStpResponse processAuditData(String auditTrdStatusId,String source) throws CisTStpException { CisTStpTradeData tradeData = new CisTStpTradeData(); List<TdsStpAudit> l = new ArrayList<>(); TdsStpResponse res = new TdsStpResponse(); try{ tradeData.setTradeId(Integer.parseInt(auditTrdStatusId)); tradeData.setSource(source); tradeData.setIsAudit("Y"); TradeProcessor processor = sourceFactory.getProcessor(tradeData.getSource()); List<MessageDetails> messageDetails = processor.processAuditTrade(tradeData); if(messageDetails!=null){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); for(MessageDetails details : messageDetails) { ProductType prodType =details.getMessage().getTrade().getTd().getProductType(); if(prodType!=null && "CURR_SWAP".equals(prodType.getCisProductType())){ continue; } TdsStpAudit o = new TdsStpAudit(); o.setCisTradeId(""+details.getMessage().getTrade().getTh().getCisTradeId()); o.setScbmlPackageId(""+details.getMessage().getTrade().getTh().getScbTradeId()); o.setMurexPackageId(""+details.getMessage().getTrade().getTh().getMxLinkId()); o.setTradeDate(details.getMessage().getTrade().getTh().getTradeDate().toString().substring(0,10)); if("SIP".equals(source)){ if(details!=null && details.getEventDetails()!=null){ if(details.getEventDetails().getDrawDown()!=null && details.getEventDetails().getDrawDown()){ if(details.getEventDetails().getEventData() instanceof DDeData){ DDeData d = (DDeData) details.getEventDetails().getEventData(); if(d!=null && d.getOriginalMsg()!=null){ o.setMurexPackageId(""+d.getOriginalMsg().getTrade().getTh().getMxLinkId()); if(d.getOriginalMsg().getTrade().getTh()!=null){ TradeHeader th = d.getOriginalMsg().getTrade().getTh(); o.setTradeDate(th.getTradeDate().toString().substring(0,10)); } } } } if(details.getEventDetails().getExpiry()!=null && details.getEventDetails().getExpiry()){ EXPeData d = (EXPeData) details.getEventDetails().getEventData(); if(d!=null && d.getOriginalMsg()!=null){ if(d.getOriginalMsg().getTrade()!=null){ if(d.getOriginalMsg().getTrade().getTh()!=null){ TradeHeader th = d.getOriginalMsg().getTrade().getTh(); o.setTradeDate(th.getTradeDate().toString().substring(0,10)); } } } } } } o.setCounterpartyFmId((details.getMessage().getCpFmId()==null?"":details.getMessage().getCpFmId())); o.setLegalEntity(details.getMessage().getLegalEntity()); o.setProductFullName(details.getMessage().getTrade().getTd().getProductType().getProductFullName()==null?"":details.getMessage().getTrade().getTd().getProductType().getProductFullName()); o.setCisEventName(details.getEventDetails().getRefEvent().getCisEvent()); o.setCisTradeStateName(details.getMessage().getCisState()); o.setPortfolioName(details.getMessage().getPort1Name()); o.setPortfolioName2(details.getMessage().getPort2Name()); l.add(o); } }else{ log.info("No message details skip this trade id: " + auditTrdStatusId); } }catch(Exception ex){ log.info("Skip this trade id: " + auditTrdStatusId); log.info(ex.getMessage()); } res.setTdsStpAuditList(l); return res; }
06-07
Sure! Here is an example of a unit test that you can write for the given method: ``` @Test public void testProcessAuditData() throws CisTStpException { // create mock objects CisTStpTradeData tradeDataMock = mock(CisTStpTradeData.class); TradeProcessor processorMock = mock(TradeProcessor.class); MessageDetails messageDetailsMock = mock(MessageDetails.class); EventDetails eventDetailsMock = mock(EventDetails.class); DDeData dDeDataMock = mock(DDeData.class); TdsStpAudit tdsStpAuditMock = mock(TdsStpAudit.class); // set up mock objects when(tradeDataMock.getTradeId()).thenReturn(1); when(tradeDataMock.getSource()).thenReturn("source"); when(tradeDataMock.getIsAudit()).thenReturn("Y"); when(processorMock.processAuditTrade(tradeDataMock)).thenReturn(Arrays.asList(messageDetailsMock)); when(messageDetailsMock.getMessage()).thenReturn(new Message()); when(messageDetailsMock.getEventDetails()).thenReturn(eventDetailsMock); when(messageDetailsMock.getCpFmId()).thenReturn("cpFmId"); when(messageDetailsMock.getLegalEntity()).thenReturn("legalEntity"); when(messageDetailsMock.getPort1Name()).thenReturn("port1Name"); when(messageDetailsMock.getPort2Name()).thenReturn("port2Name"); when(eventDetailsMock.getDrawDown()).thenReturn(true); when(eventDetailsMock.getEventData()).thenReturn(dDeDataMock); when(eventDetailsMock.getRefEvent()).thenReturn(new RefEvent()); when(eventDetailsMock.getRefEvent().getCisEvent()).thenReturn("cisEvent"); when(dDeDataMock.getOriginalMsg()).thenReturn(new Message()); when(tdsStpAuditMock.getCisTradeId()).thenReturn("cisTradeId"); when(tdsStpAuditMock.getScbmlPackageId()).thenReturn("scbmlPackageId"); when(tdsStpAuditMock.getMurexPackageId()).thenReturn("murexPackageId"); when(tdsStpAuditMock.getTradeDate()).thenReturn("2022-01-01"); when(tdsStpAuditMock.getCounterpartyFmId()).thenReturn("cpFmId"); when(tdsStpAuditMock.getLegalEntity()).thenReturn("legalEntity"); when(tdsStpAuditMock.getProductFullName()).thenReturn("productFullName"); when(tdsStpAuditMock.getCisEventName()).thenReturn("cisEvent"); when(tdsStpAuditMock.getCisTradeStateName()).thenReturn("cisTradeStateName"); when(tdsStpAuditMock.getPortfolioName()).thenReturn("port1Name"); when(tdsStpAuditMock.getPortfolioName2()).thenReturn("port2Name"); List<TdsStpAudit> expectedList = Arrays.asList(tdsStpAuditMock); TdsStpResponse expectedResult = new TdsStpResponse(); expectedResult.setTdsStpAuditList(expectedList); // create the class under test and inject mock objects MyClass myClass = new MyClass(); myClass.setSourceFactory(new SourceFactory() { @Override public TradeProcessor getProcessor(String source) { return processorMock; } }); // execute the method under test TdsStpResponse result = myClass.processAuditData("1", "source"); // assert the result assertEquals(expectedResult.getTdsStpAuditList().size(), result.getTdsStpAuditList().size()); assertEquals(expectedResult.getTdsStpAuditList().get(0), result.getTdsStpAuditList().get(0)); } ``` In this test, we are mocking all the necessary objects (such as `CisTStpTradeData`, `TradeProcessor`, `MessageDetails`, etc.) and setting up their behaviors using the `Mockito` library. We then create an instance of the class under test (`MyClass`) and inject the mock objects into it. Finally, we call the `processAuditData()` method with some sample input and assert that the output matches the expected result. Please note that this is just an example, and you may need to modify the test to fit your specific requirements.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值