java - How check if throwed exception -
if have this:
public user finduserbyemail(string email) throws customernotfoundexception{ list<user> users = new arraylist<user>(); users = sessionfactory.getcurrentsession().createquery("from user email = ?").setparameter(0,email).list(); if (users.size() > 0) { return users.get(0); } else { throw new customernotfoundexception(); } }
and in moment want check returned finduserbyemail(string email) method whether return user object or customernotfoundexception in end.
i tried in way
private boolean searchcustomer(string email) throws customernotfoundexception{ if (hibernatedao.finduserbyemail(email).getclass() == user.class) { .... } else { .... } }
is way or there betters?
no. nonononono.
use catch
keyword catch exception
.
try { user thingie = hibernatedao.finduserbyemail(email); } catch (customernotfoundexception cnfe) { // todo logic on failure }
also remove throws
statement searchcustomer
method's signature if you're using try
/ catch
mechanism , not rethrowing exception
.
Comments
Post a Comment