Skip to content

8353713: Improve Currency.getInstance exception handling #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/java.base/share/classes/java/util/Currency.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ private static Currency getInstance(String currencyCode, int defaultFractionDigi
// or in the list of other currencies.
boolean found = false;
if (currencyCode.length() != 3) {
throw new IllegalArgumentException("The input currency code must " +
"have a length of 3 characters");
throw new IllegalArgumentException("The input currency code '" + currencyCode +
"' must have a length of 3 characters");
}
char char1 = currencyCode.charAt(0);
char char2 = currencyCode.charAt(1);
Expand All @@ -335,8 +335,8 @@ private static Currency getInstance(String currencyCode, int defaultFractionDigi
if (!found) {
OtherCurrencyEntry ocEntry = OtherCurrencyEntry.findEntry(currencyCode);
if (ocEntry == null) {
throw new IllegalArgumentException("The input currency code" +
" is not a valid ISO 4217 code");
throw new IllegalArgumentException("The input currency code'" + currencyCode +
"' is not a valid ISO 4217 code");
}
defaultFractionDigits = ocEntry.fraction;
numericCode = ocEntry.numericCode;
Expand Down Expand Up @@ -391,8 +391,8 @@ public static Currency getInstance(Locale locale) {
String country = CalendarDataUtility.findRegionOverride(locale).getCountry();

if (country == null || !country.matches("^[a-zA-Z]{2}$")) {
throw new IllegalArgumentException("The country of the input locale" +
" is not a valid ISO 3166 country code");
throw new IllegalArgumentException("The country of the input locale '" + locale +
"' is not a valid ISO 3166 country code");
}

char char1 = country.charAt(0);
Expand All @@ -409,8 +409,8 @@ public static Currency getInstance(Locale locale) {
} else {
// special cases
if (tableEntry == INVALID_COUNTRY_ENTRY) {
throw new IllegalArgumentException("The country of the input locale" +
" is not a valid ISO 3166 country code");
throw new IllegalArgumentException("The country of the input locale '" + locale +
"' is not a valid ISO 3166 country code");
}
if (tableEntry == COUNTRY_WITHOUT_CURRENCY_ENTRY) {
return null;
Expand Down
17 changes: 9 additions & 8 deletions test/jdk/java/util/Currency/CurrencyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ private static Stream<String> validCurrencies() {
public void invalidCurrencyTest(String currencyCode) {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () ->
Currency.getInstance(currencyCode), "getInstance() did not throw IAE");
assertEquals("The input currency code is not a" +
" valid ISO 4217 code", ex.getMessage());
assertEquals("The input currency code'" + currencyCode +
"' is not a valid ISO 4217 code", ex.getMessage());
}

private static Stream<String> non4217Currencies() {
Expand All @@ -99,8 +99,8 @@ private static Stream<String> non4217Currencies() {
public void invalidCurrencyLengthTest(String currencyCode) {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () ->
Currency.getInstance(currencyCode), "getInstance() did not throw IAE");
assertEquals("The input currency code must have a length of 3" +
" characters", ex.getMessage());
assertEquals("The input currency code '" + currencyCode +
"' must have a length of 3 characters", ex.getMessage());
}

private static Stream<String> invalidLengthCurrencies() {
Expand Down Expand Up @@ -163,8 +163,8 @@ public void localeMappingTest() {
"AC|CP|DG|EA|EU|FX|IC|SU|TA|UK")) { // exceptional reservation codes
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> Currency.getInstance(locale), "Did not throw IAE");
assertEquals("The country of the input locale is not a" +
" valid ISO 3166 country code", ex.getMessage());
assertEquals("The country of the input locale '" + locale + "' is not a " +
"valid ISO 3166 country code", ex.getMessage());
} else {
goodCountries++;
Currency currency = Currency.getInstance(locale);
Expand All @@ -183,10 +183,11 @@ public void localeMappingTest() {
// Check an invalid country code
@Test
public void invalidCountryTest() {
Locale locale = Locale.of("", "EU");
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
()-> Currency.getInstance(Locale.of("", "EU")), "Did not throw IAE");
assertEquals("The country of the input locale is not a valid" +
" ISO 3166 country code", ex.getMessage());
assertEquals("The country of the input locale '" + locale +
"' is not a valid ISO 3166 country code", ex.getMessage());
}

// Ensure a selection of countries have the expected currency
Expand Down