Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.Currency.getInstance throwing IllegalArgumentException

Tags:

java

I am just trying my hands around with java.util.Currency, but for few currency codes its throwing me java.lang.IllegalArgumentException

this is my simple code

java.util.Currency c = java.util.Currency.getInstance(code);

as per java doc

public static Currency getInstance(String currencyCode)

Returns the Currency instance for the given currency code.

Parameters:
    currencyCode - the ISO 4217 code of the currency 
Returns:
    the Currency instance for the given currency code 
Throws:
    NullPointerException - if currencyCode is null 
    IllegalArgumentException - if currencyCode is not a supported ISO 4217 code.

I am getting this exception for few codes and few of them are

  1. UYI
  2. COU
  3. TMT

as per ISO_4217, all above codes are official ISO 4217 currency names, i am not sure why i am getting this exception in this case? Do i need to do something else or need to pass some other information to JVM to get correct values? any help to clear my confusion is really appreciable or can point me to the ISO 4217 currency which java doc is pointing as i can see above codes in ISO 4217 list so its possible i am looking at wrong documents/references.

In order to provide more insights here is the stack trace

Exception in thread "main" java.lang.IllegalArgumentException
    at java.util.Currency.getInstance(Unknown Source)
    at java.util.Currency.getInstance(Unknown Source)
    at CurrencyTest1.main(CurrencyTest1.java:10)

and Java version

java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b07)
Java HotSpot(TM) 64-Bit Server VM (build 17.0-b17, mixed mode)
like image 512
Umesh Awasthi Avatar asked Oct 06 '12 16:10

Umesh Awasthi


2 Answers

This is an interesting problem.

Both COU and UYI are not real currencies. COU is the old Venezuelan currency (not in use anymore) and UYI is an indexed currency, probably used by banks to sort out exchange rates.

In java 1.7.0_u7, the currecy TMT works fine. TMT was introduced in 2009, so it makes sense that Java 6 doesn't know that code.

like image 167
Augusto Avatar answered Oct 23 '22 01:10

Augusto


For Uruguay UYU is working fine instead of UYI and COP instead of COU for Columbia TMT is not working for me also.

like image 23
Abubakkar Avatar answered Oct 23 '22 00:10

Abubakkar