The exclusive promotional activities on JCOP J3H145 card and Feitian R301 Smartcard Reader are in full swing. Please check this page for details.

How to throw an exception in my applet

Applets Development Guide

Moderator: product

CarCard
Posts: 1
Joined: Thu May 10, 2018 5:30 am
Points :14
Contact:

How to throw an exception in my applet

Post by CarCard » Thu May 10, 2018 5:35 am

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?

Garfield
Posts: 3
Joined: Thu Sep 22, 2016 4:15 am
Points :58
Contact:

Re: How to throw an exception in my applet

Post by Garfield » Tue Jun 12, 2018 3:10 am

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.

mjalali1365
Posts: 6
Joined: Tue Jun 25, 2019 3:19 am
Points :94
Contact:

Re: How to throw an exception in my applet

Post by mjalali1365 » 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?

kuafu
Posts: 189
Joined: Thu Jun 25, 2015 2:09 am
Points :2181
Contact:

Re: How to throw an exception in my applet

Post by kuafu » 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);
well

mjalali1365
Posts: 6
Joined: Tue Jun 25, 2019 3:19 am
Points :94
Contact:

Re: How to throw an exception in my applet

Post by mjalali1365 » 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
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?

Garfield
Posts: 3
Joined: Thu Sep 22, 2016 4:15 am
Points :58
Contact:

Re: How to throw an exception in my applet

Post by Garfield » Wed Jul 17, 2019 6:31 am

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();
}

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 3 guests

JavaCard OS : Disclaimer