虽然Java Card不推荐递归编程风格,但我想对Fibonacci算法做一个小测试。我编写了一个函数来计算大整数的斐波那契套件(由字节数组表示)。
我的代码如下:
public static byte[] fibonacci(byte[] n) {
if (isLEThan1(n)) {
return n;
}
else {
return add(fibonacci(subtract(n, new byte[]{0x01})),fibonacci(subtract(n,new byte[]{0x02})));
}
}
哪里boolean isLEThan(byte[])
返回 true
如果字节数组表示的整数小于或等于1,false
如果没有。
byte[] add(byte[], byte[])
和 byte[] subtract(byte[], byte[])
为字节数组表示的大整数实现加法和减法。它们返回一个包含操作结果的新字节数组。
我认为通过为上述函数提供一个大数组,我会得到一个异常,例如 SystemException.NO_RESOURCE
由于递归调用减法实例化的数组数量。
但我不得不认为我没有捕捉到正确的异常,因为我得到了 6F00
作为状态词。
以下是我认为的异常(exception)情况列表:
try {
fibonacci(array);
} catch (ArithmeticException e) {
ISOException.throwIt((short) 0x0100);
} catch (ArrayStoreException e) {
ISOException.throwIt((short) 0x0200);
} catch (APDUException e) {
ISOException.throwIt(Util.makeShort((byte) 0x03,
(byte) e.getReason()));
} catch (CryptoException e) {
ISOException.throwIt(Util.makeShort((byte) 0x04,
(byte) e.getReason()));
} catch (ISOException e) {
ISOException.throwIt(Util.makeShort((byte) 0x05,
(byte) e.getReason()));
} catch (PINException e) {
ISOException.throwIt(Util.makeShort((byte) 0x06,
(byte) e.getReason()));
} catch (ServiceException e) {
ISOException.throwIt(Util.makeShort((byte) 0x07,
(byte) e.getReason()));
} catch (SystemException e) {
ISOException.throwIt(Util.makeShort((byte) 0x08,
(byte) e.getReason()));
} catch (TransactionException e) {
ISOException.throwIt(Util.makeShort((byte) 0x09,
(byte) e.getReason()));
} catch (ClassCastException e) {
ISOException.throwIt((short) 0x0A00);
} catch (IndexOutOfBoundsException e) {
ISOException.throwIt((short) 0x0B00);
} catch (NegativeArraySizeException e) {
ISOException.throwIt((short) 0x0C00);
} catch (NullPointerException e) {
ISOException.throwIt((short) 0x0D00);
} catch (SecurityException e) {
ISOException.throwIt((short) 0x0E00);
}
}
那么,有人知道那种情况下所涉及的异常吗?
最佳答案
这是一个SystemException
。但是,ISO/IEC 7816-4 不允许您只使用任何状态字。而是使用例如(短)(0x6700 | 0x0080 | e.getReason())
作为您的 ISOException
的原因。
关于java - 在 Java Card 递归编程期间捕获内存异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32438760/