Page 1 of 1

How to throw an exception in my applet

Posted: Thu May 10, 2018 5:35 am
by CarCard
Hi, everyone

I'm learning to write Java Card Applet.

I want to throw an exception when the program execution error occurs, How to write the code in my applet?

Can anyone help me?

Re: How to throw an exception in my applet

Posted: Tue Jun 12, 2018 3:10 am
by Garfield
There is a special function for exception handling in class "ISOException":
public static void throwIt(short sw)
{}
you could use it.
Firstly, you need to import the class "ISOException" in your Applet by using the command below:
"import javacard.framework.ISOException;"
next step, call the function when you need,such as:
if(condition == false)
{
ISOException.throwIt((short)0x6F00);
}

Hope it helps you.

Re: How to throw an exception in my applet

Posted: Wed Jul 17, 2019 2:09 am
by mjalali1365
Garfield wrote:
Tue Jun 12, 2018 3:10 am
if(condition == false)
{
ISOException.throwIt((short)0x6F00);
}
Does it only throw 0x6F00? How one can throw the reason for an specific exception?

Re: How to throw an exception in my applet

Posted: Wed Jul 17, 2019 4:54 am
by kuafu
mjalali1365 wrote:
Wed Jul 17, 2019 2:09 am
Garfield wrote:
Tue Jun 12, 2018 3:10 am
if(condition == false)
{
ISOException.throwIt((short)0x6F00);
}
Does it only throw 0x6F00? How one can throw the reason for an specific exception?
you can throw any short value .
such as ISOException.throwIt((short)0x6899); or ISOException.throwIt((short)0x2222);

Re: How to throw an exception in my applet

Posted: Wed Jul 17, 2019 6:06 am
by mjalali1365
kuafu wrote:
Wed Jul 17, 2019 4:54 am
mjalali1365 wrote:
Wed Jul 17, 2019 2:09 am
Garfield wrote:
Tue Jun 12, 2018 3:10 am
if(condition == false)
{
ISOException.throwIt((short)0x6F00);
}
Does it only throw 0x6F00? How one can throw the reason for an specific exception?
you can throw any short value .
such as ISOException.throwIt((short)0x6899); or ISOException.throwIt((short)0x2222);
suppose that I do an encryption without first generating the key (run time error), does'nt the function generate an exception to be thrown?

Re: How to throw an exception in my applet

Posted: Wed Jul 17, 2019 6:31 am
by Garfield
mjalali1365 wrote:
Wed Jul 17, 2019 6:06 am
kuafu wrote:
Wed Jul 17, 2019 4:54 am
mjalali1365 wrote:
Wed Jul 17, 2019 2:09 am


Does it only throw 0x6F00? How one can throw the reason for an specific exception?
you can throw any short value .
such as ISOException.throwIt((short)0x6899); or ISOException.throwIt((short)0x2222);
suppose that I do an encryption without first generating the key (run time error), does'nt the function generate an exception to be thrown?
Any exception that beyond your code logic will be catched by JAVA virtual machine automatically;
if you want to do it by yourself, please refer to the following example below:
try
{
int x=d.div(4,0);
System.out.println("x="+x);
}
catch(Exception e)
{
System.out.println("error message");
System.out.println(e.getMessage());
System.out.println(e.toString());
e.printStackTrace();
}