There is a better approach for this. if you see a JWT exception handler object, for example. ExpiredJwtException, the expection object itself contains the following: header, claims, and message
therefore, statements can be easily retrieved through this object, i.e. e.getClaims().getId() , where e is an ExpiredJwtException object.
ExpiredJwtException consturct is as follows: -
public ExpiredJwtException(Header header, Claims claims, String message) { super(header, claims, message); }
Example: -
try{ // executable code }catch(ExpiredJwtException e){ System.out.println("token expired for id : " + e.getClaims().getId()); }
source share