4

I am creating a button that will allow System Admins to reset user passwords via System.resetPassword. We have password restrictions setup such that a password must exist for 24 hours. Therefore, if the button is clicked twice in the same day, an "UNKNOWN_EXCEPTION: Your password cannot be reset at this time. Please contact your organization's administrator for more information." is thrown to the page.

I was hoping to be able to catch this error, however even when I put a try catch around it, the error still comes up on the page. Is there a special way to catch this exception?

for (User u : users) {
    try {
        System.resetPassword(u.Id, true);
    }
    catch (Exception e) {
        usersUnableToReset += u.Username;
        continue;
    }
    usersAbleToReset += u.Username;
}

1 Answer 1

7

No, UnexpectedException, AssertException, and LimitException are all uncatchable.

Easy enough to verify via Execute Anonymous:

try
{
    throw new UnexpectedException();
}
catch(UnexpectedException e) { }
3
  • Thanks for the answer, that's what it appeared to me from my tests. Do you have any background as to why those are uncatchable? Dec 12, 2019 at 21:59
  • Well, for the LimitExceptionif it was catchable, then it wouldn't really work as a limit... For AssertException I think it's supposed to break stuff if the assertion is not true. Dec 13, 2019 at 0:05
  • 2
    Those cases make sense to me, but the password reset UnknownException doesn't really. I want to try to reset the User's password, if I get an exception due to it already changing in the last 24 hours, I simply want to catch it, log the user, and then continue trying to reset the rest of the users. Dec 13, 2019 at 13:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .