Hi,
The two should behave the same as long as the issue happens within the context manager. If it comes from validation/saving, then method 1 might miss it.
Also your training is most likely not deterministic so if the nan appear due to the optimization behavior, they might appear at different time of the training.
The second one should allow you to find and fix the issue no?
Also a good sanity check is to monitor the loss and make sure it does not diverge (as nan can be caused by a loss that diverged).