better exceptions v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 15 Dec 2013 23:58:58 +0100
branchv_0
changeset 9 2ec52027b97f
parent 8 4507cb9a0cf1
child 10 f528406f33f4
better exceptions
java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java
java/sql-dk/src/info/globalcode/sql/dk/CLIParserException.java
java/sql-dk/test/info/globalcode/sql/dk/CLIParserTest.java
--- a/java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java	Sun Dec 15 23:54:51 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java	Sun Dec 15 23:58:58 2013 +0100
@@ -27,7 +27,7 @@
 		types = Collections.unmodifiableMap(m);
 	}
 
-	public CLIOptions parseOptions(String[] args) {
+	public CLIOptions parseOptions(String[] args) throws CLIParserException {
 		CLIOptions options = new CLIOptions();
 
 		List<Integer> numberedTypes = new ArrayList<>();
@@ -86,9 +86,9 @@
 								try {
 									paramType = numberedTypes.get(paramIndex);
 								} catch (IndexOutOfBoundsException e) {
-									throw new IllegalArgumentException("Missing type for parameter #" + paramIndex, e);
+									throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
 								} catch (NullPointerException e) {
-									throw new IllegalArgumentException("Invalid type definition for parameter #" + paramIndex, e);
+									throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
 								}
 								parameter = new Parameter(arg, paramType);
 							}
@@ -97,17 +97,17 @@
 					}
 					break;
 				default:
-					throw new IllegalArgumentException("Unknown option: " + arg);
+					throw new CLIParserException("Unknown option: " + arg);
 			}
 		}
 		return options;
 	}
 
-	private String fetchNext(String[] args, int index) {
+	private String fetchNext(String[] args, int index) throws CLIParserException {
 		if (index < args.length) {
 			return args[index];
 		} else {
-			throw new IllegalArgumentException("Expecting value for option: " + args[index - 1]);
+			throw new CLIParserException("Expecting value for option: " + args[index - 1]);
 		}
 	}
 
@@ -126,10 +126,10 @@
 		}
 	}
 
-	private int getType(String typeString) {
+	private int getType(String typeString) throws CLIParserException {
 		Integer type = types.get(typeString);
 		if (type == null) {
-			throw new IllegalArgumentException("Unsupported type: " + typeString);
+			throw new CLIParserException("Unsupported type: " + typeString);
 		} else {
 			return type;
 		}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIParserException.java	Sun Dec 15 23:58:58 2013 +0100
@@ -0,0 +1,23 @@
+package info.globalcode.sql.dk;
+
+/**
+ *
+ * @author Ing. František Kučera (frantovo.cz)
+ */
+public class CLIParserException extends Exception {
+
+	public CLIParserException() {
+	}
+
+	public CLIParserException(String message) {
+		super(message);
+	}
+
+	public CLIParserException(Throwable cause) {
+		super(cause);
+	}
+
+	public CLIParserException(String message, Throwable cause) {
+		super(message, cause);
+	}
+}
--- a/java/sql-dk/test/info/globalcode/sql/dk/CLIParserTest.java	Sun Dec 15 23:54:51 2013 +0100
+++ b/java/sql-dk/test/info/globalcode/sql/dk/CLIParserTest.java	Sun Dec 15 23:58:58 2013 +0100
@@ -24,7 +24,7 @@
 	}
 
 	@Test
-	public void testParseOptions_QueryNow_Numbered() throws InvalidOptionsException {
+	public void testParseOptions_QueryNow_Numbered() throws InvalidOptionsException, CLIParserException {
 		String[] args = new String[]{Tokens.DB, DATABASE_NAME_1, Tokens.SQL, SQL_1, Tokens.DATA, DATA_1, DATA_2, DATA_3};
 		CLIOptions options = parser.parseOptions(args);
 		options.validate();
@@ -42,8 +42,8 @@
 	}
 
 	@Test
-	public void testParseOptions_QueryNow_Named() throws InvalidOptionsException {
-		String[] args = new String[]{Tokens.DB, DATABASE_NAME_1, Tokens.SQL, SQL_1, Tokens.TYPES};
+	public void testParseOptions_QueryNow_Named() throws InvalidOptionsException, CLIParserException {
+		String[] args = new String[]{Tokens.DB, DATABASE_NAME_1, Tokens.SQL, SQL_1};
 		CLIOptions options = parser.parseOptions(args);
 		options.validate();
 
@@ -53,7 +53,7 @@
 	}
 
 	@Test
-	public void testParseOptions_PrepareBatch() throws InvalidOptionsException {
+	public void testParseOptions_PrepareBatch() throws InvalidOptionsException, CLIParserException {
 		String[] args = new String[]{Tokens.BATCH, Tokens.SQL, SQL_1};
 		CLIOptions options = parser.parseOptions(args);
 		options.validate();
@@ -63,7 +63,7 @@
 	}
 
 	@Test
-	public void testParseOptions_ExecuteBatch() throws InvalidOptionsException {
+	public void testParseOptions_ExecuteBatch() throws InvalidOptionsException, CLIParserException {
 		String[] args = new String[]{Tokens.BATCH, Tokens.DB, DATABASE_NAME_1};
 		CLIOptions options = parser.parseOptions(args);
 		options.validate();