The usual way to get the behaviour you want is to have your swapElements throw the exception back to the caller and make them handle it and not your swapElements method. Contrived example:

...