[英]ClassCastException in spring
我試圖捕獲異常並向我的客戶端頁面返回一個值,但是我得到ClassCastException
並且它不返回任何值。
我的代碼段
@RequestMapping(value = "/config/MyFile/data", method = RequestMethod.GET)
@ResponseBody
public List<Myfile> getAllFlatFileTrafficCops()
{
try
{
return MyFileServices.getAlldata();
}
catch (final ResourceAccessException r)
{
final Throwable flatfileTrafficCopStatus;
flatfileTrafficCopStatus = r.getCause();
return (List<FlatFileTrafficCop>) flatfileTrafficCopStatus;
}
}
在這種情況下,您可以在“ Exception
情況下采取的所有措施將Exception
捕獲,並將其封裝在有意義的自定義異常中,並將其傳播回調用方函數,並在Web上顯示有意義的錯誤文本。
始終將異常傳播回調用者。 對於調試,您可能要在那兒添加一條log
語句。
catch (final ResourceAccessException r)
{
CustomException cException = new CustomException(r);
throw cException;
//OR
FlatFileTrafficCop emptyObj= new FlatFileTrafficCop(Witherr);
//add that in a list and return
}
首先是ResourceAccessException
ClassCastException
子級嗎? 不,所以您的捕獲將無法工作,如果您想捕獲ClassCastException
,則必須對其進行第二次捕獲
catch (final ClassCastException e){}
但是當您使用spring時,不需要像這樣捕獲它,可以使用@ExceptionHandler代替
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.