1

I am doing code review on some old code and I found that we are checking if a session exists. Below is an example:

ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();

HttpSession session = (HttpSession) ec.getSession(false);

if(session!=null)
{...}
else
{ ..}

My question is would an active context, the context of the current request, ever be without a session?

My thought is whether I should allow an error to be thrown if there is no session, or if a request without a session is a normal thing captured appropriately by the if/else statement.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Gratus D.
  • 787
  • 7
  • 22
  • Wouldn't the cast also throw a `NullPointerException` if `ec.getSession(false)` returned null? – Robert Oct 31 '17 at 14:28
  • @Robert based on this: https://stackoverflow.com/questions/24208612/casting-null-to-any-type it would seem null can be cast to anything. – Gratus D. Oct 31 '17 at 14:33

0 Answers0