除非我捕獲並重新捕獲Lambda表達式中的各種異常,否則下面的代碼不會編譯。任何人都可以幫我理解爲什麼?Servlet中的Java Lambda表達式
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
Consumer <String> forwardTo =(s) ->
{
RequestDispatcher rd= req.getRequestDispatcher(s);
try {
rd.forward(req, res);
} catch (IOException|ServletException is) {
try {
throw new Exception(is);
} catch (Exception e) {
}
}
};
我用javac編譯1.8.0_66 。真正的問題是,自從doGet拋出這些異常,爲什麼我必須再次捕捉它們呢? – Jerrolds
Lambdas是函數,它們需要自己的異常處理。 Althouhh'doGet'拋出'IOException',它沒有考慮在lambdas中拋出的異常 –
我明白,但req和res是doGet傳遞的。 – Jerrolds