Make small improvements to exceptions

This commit is contained in:
Zafar Khaja 2014-06-23 21:24:23 +03:00
parent 77fd6f4be1
commit e98a07dd5e
3 changed files with 38 additions and 4 deletions

View File

@ -31,6 +31,13 @@ package com.github.zafarkhaja.semver;
*/
public class ParseException extends RuntimeException {
/**
* Constructs a {@code ParseException} instance with no error message.
*/
public ParseException() {
super();
}
/**
* Constructs a {@code ParseException} instance with an error message.
*
@ -41,9 +48,30 @@ public class ParseException extends RuntimeException {
}
/**
* Constructs a {@code ParseException} instance with no error message.
* Constructs a {@code ParseException} instance with an error message
* and the cause exception.
*
* @param message the error message
* @param cause an exception that caused this exception
*/
public ParseException() {
public ParseException(String message, UnexpectedCharacterException cause) {
super(message);
initCause(cause);
}
/**
* Returns the string representation of this exception.
*
* @return the string representation of this exception
*/
@Override
public String toString() {
Throwable cause = getCause();
String msg = getMessage();
if (msg != null) {
msg += ((cause != null) ? " (" + cause.toString() + ")" : "");
return msg;
}
return ((cause != null) ? cause.toString() : "");
}
}

View File

@ -66,7 +66,10 @@ public class UnexpectedTokenException extends ParseException {
*/
@Override
public String toString() {
String message = String.format("Unexpected token '%s'", unexpected);
String message = String.format(
"Unexpected token '%s'",
unexpected
);
if (expected.length > 0) {
message += String.format(
", expecting '%s'",

View File

@ -84,7 +84,10 @@ public class UnexpectedElementException extends RuntimeException {
*/
@Override
public String toString() {
String message = String.format("Unexpected element '%s'", unexpected);
String message = String.format(
"Unexpected element '%s'",
unexpected
);
if (expected.length > 0) {
message += String.format(
", expecting '%s'",