1 /** 2 ****************************************************************************** 3 * Copyright (C) 1996-2005, International Business Machines Corporation and * 4 * others. All Rights Reserved. * 5 ****************************************************************************** 6 * 7 ****************************************************************************** 8 */ 9 10 package libcore.icu; 11 12 /** 13 * Error exception class mapping ICU error codes of the enum UErrorCode 14 * @author syn wee quek 15 */ 16 public final class ErrorCode extends Exception { isFailure(int error)17 public static boolean isFailure(int error) { 18 return error > U_ZERO_ERROR && error < U_ERROR_LIMIT; 19 } 20 throwException(int error)21 public static RuntimeException throwException(int error) { 22 if (error <= U_ZERO_ERROR && error >= U_ERROR_LIMIT) { 23 return null; 24 } 25 switch (error) { 26 case U_ILLEGAL_ARGUMENT_ERROR: 27 return new IllegalArgumentException(ERROR_NAMES[error]); 28 case U_INDEX_OUTOFBOUNDS_ERROR: 29 case U_BUFFER_OVERFLOW_ERROR: 30 return new ArrayIndexOutOfBoundsException(ERROR_NAMES[error]); 31 case U_UNSUPPORTED_ERROR: 32 return new UnsupportedOperationException(ERROR_NAMES[error]); 33 } 34 throw new RuntimeException(ERROR_NAMES[error]); 35 } 36 37 // The errors needed by our CharsetDecoderICU/CharsetEncoderICU. 38 public static final int U_ZERO_ERROR = 0; 39 private static final int U_ILLEGAL_ARGUMENT_ERROR = 1; 40 private static final int U_INDEX_OUTOFBOUNDS_ERROR = 8; 41 public static final int U_INVALID_CHAR_FOUND = 10; 42 public static final int U_TRUNCATED_CHAR_FOUND = 11; 43 public static final int U_ILLEGAL_CHAR_FOUND = 12; 44 public static final int U_BUFFER_OVERFLOW_ERROR = 15; 45 private static final int U_UNSUPPORTED_ERROR = 16; 46 private static final int U_ERROR_LIMIT = 21; 47 48 // TODO: this list is incomplete; get these from native code! 49 private static final String ERROR_NAMES[] = { 50 "U_ZERO_ERROR", 51 "U_ILLEGAL_ARGUMENT_ERROR", 52 "U_MISSING_RESOURCE_ERROR", 53 "U_INVALID_FORMAT_ERROR", 54 "U_FILE_ACCESS_ERROR", 55 "U_INTERNAL_PROGRAM_ERROR", 56 "U_MESSAGE_PARSE_ERROR", 57 "U_MEMORY_ALLOCATION_ERROR", 58 "U_INDEX_OUTOFBOUNDS_ERROR", 59 "U_PARSE_ERROR", 60 "U_INVALID_CHAR_FOUND", 61 "U_TRUNCATED_CHAR_FOUND", 62 "U_ILLEGAL_CHAR_FOUND", 63 "U_INVALID_TABLE_FORMAT", 64 "U_INVALID_TABLE_FILE", 65 "U_BUFFER_OVERFLOW_ERROR", 66 "U_UNSUPPORTED_ERROR", 67 "U_RESOURCE_TYPE_MISMATCH", 68 "U_ILLEGAL_ESCAPE_SEQUENCE", 69 "U_UNSUPPORTED_ESCAPE_SEQUENCE" 70 }; 71 } 72