Search code examples
javaexceptioncryptographysmartcardjavacard

How can I tell which reason code an exception has thrown in JavaCard?


I'm trying to find out why a CryptoException has been thrown in my applet but I'm struggling.

I've tried this:

catch (CryptoException e) {
    if (e instanceof CryptoException.ILLEGAL_USE) {
        //send certain apdu
    }
}

And this:

catch (CryptoException e) {
    if (e == (short)5) {
    //send certain apdu
    }
}

And also this:

catch (CryptoException e) {
    if (e == CryptoException.ILLEGAL_USE) {
    //SEND CERTAIN APDU
    }
}

But none of them seem to work and I can't find the right way to use the reason code to send a specific apdu. Is there a way to do this?


Solution

  • Call getReason() on the CryptoException:

    try {
        // . . .
    } catch (CryptoException e) {
        if (e.getReason() == CryptoException.ILLEGAL_USE) {
            // SEND CERTAIN APDU
        }
    }