Does finally execute if there is a sendredirect and return.

try{ response.sendRedirect( session.getAttribute("site")+"/jsp/process/payment.jsp?oid="+session.getAttribute("OrderID")+"&term="+session.getAttribute("MerchantID")); return; }finally{ } 
  • 2
    Well, you could check it yourself. For example, something to print to the console in the finally block - arg

2 answers 2

finally is not executed only in cases:

  1. Call System.exit()
  2. Interrupts the current thread by another thread
  3. JVM Falls.
 public static int getANumber(){ try{ return 7; } finally { return 43; } } 

Calling the getANumber() method returns 43.

  • But what about response.sendRedirect, it turns out it works out translates to another page and then performs finally or finally executed before transferring to another page - J Mas
  • @Yeldos and you yourself tried to type something into the console? Very good way to debug. recommend. - arg
  • so if I redirect, it will show me another page and I’m wondering exactly about response.sendredirect - J Mas

the finally block is executed before the try return of the try block if there is a return in the finally block - the try block’s return is ignored

  • Meaning, before returning the value? Otherwise, the code in the return may have side effects ... - Qwertiy
  • and about response.sendredirect, you first redirect or finally - J Mas
  • @YeldosTanikin the try block code is executed before the keyword return (unless throwing an exception). So response.sendRedirect is first executed - Nurlan
  • means that the page is redirected and finally is also executed after the response, and if there is still response.sendredirect in finally - J Mas