Skip to content

Commit eb569b5

Browse files
committed
Merge pull request stleary#236 from madsager/master
Make nextString throw a JSONException instead of a NumberFormatExcept…
2 parents 612dafc + 16a86d7 commit eb569b5

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

JSONTokener.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,11 @@ public String nextString(char quote) throws JSONException {
278278
sb.append('\r');
279279
break;
280280
case 'u':
281-
sb.append((char)Integer.parseInt(this.next(4), 16));
281+
try {
282+
sb.append((char)Integer.parseInt(this.next(4), 16));
283+
} catch (NumberFormatException e) {
284+
throw this.syntaxError("Illegal escape.", e);
285+
}
282286
break;
283287
case '"':
284288
case '\'':
@@ -433,6 +437,16 @@ public JSONException syntaxError(String message) {
433437
return new JSONException(message + this.toString());
434438
}
435439

440+
/**
441+
* Make a JSONException to signal a syntax error.
442+
*
443+
* @param message The error message.
444+
* @param causedBy The throwable that caused the error.
445+
* @return A JSONException object, suitable for throwing
446+
*/
447+
public JSONException syntaxError(String message, Throwable causedBy) {
448+
return new JSONException(message + this.toString(), causedBy);
449+
}
436450

437451
/**
438452
* Make a printable string of this JSONTokener.

0 commit comments

Comments
 (0)