From a7a87f682a039d4761112f1dedb9351f3d7a2bbf Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Tue, 6 Oct 2020 19:26:52 -0400 Subject: Rename exceptions to not use Exception This renames several of the stock exceptions to remove 'Exception' from their name. This is something that I don't recall where I picked it up from, but the idea is that the fact that it is an exception is generally syntactically obvious. Therefore, you can instead give your exceptions names which directly state what it wrong, as well as strongly implying what you can do to fix it. For instance, if you get a DirectionInvalid, it is apparent what the issue is (it is less apparent why it is invalid, but that is another issue, and a discussion for another time) --- .../java/bjc/utils/exceptions/BadPragmaFormat.java | 28 ++++++++++++++++++++++ .../bjc/utils/exceptions/DirectionInvalid.java | 26 ++++++++++++++++++++ .../utils/exceptions/FileNotChosenException.java | 28 ---------------------- .../exceptions/InvalidDirectionException.java | 26 -------------------- .../java/bjc/utils/exceptions/NoFileChosen.java | 28 ++++++++++++++++++++++ .../utils/exceptions/PragmaFormatException.java | 28 ---------------------- .../java/bjc/utils/exceptions/UnknownPragma.java | 24 +++++++++++++++++++ .../utils/exceptions/UnknownPragmaException.java | 24 ------------------- .../main/java/bjc/utils/gui/SimpleFileChooser.java | 18 +++++++------- .../bjc/utils/ioutils/RuleBasedConfigReader.java | 4 ++-- .../bjc/utils/ioutils/RuleBasedReaderPragmas.java | 10 ++++---- base/src/main/java/bjc/utils/misc/Direction.java | 8 +++---- .../java/bjc/utils/misc/RelativeDirection.java | 4 ++-- 13 files changed, 128 insertions(+), 128 deletions(-) create mode 100644 base/src/main/java/bjc/utils/exceptions/BadPragmaFormat.java create mode 100644 base/src/main/java/bjc/utils/exceptions/DirectionInvalid.java delete mode 100644 base/src/main/java/bjc/utils/exceptions/FileNotChosenException.java delete mode 100644 base/src/main/java/bjc/utils/exceptions/InvalidDirectionException.java create mode 100644 base/src/main/java/bjc/utils/exceptions/NoFileChosen.java delete mode 100644 base/src/main/java/bjc/utils/exceptions/PragmaFormatException.java create mode 100644 base/src/main/java/bjc/utils/exceptions/UnknownPragma.java delete mode 100644 base/src/main/java/bjc/utils/exceptions/UnknownPragmaException.java (limited to 'base/src/main/java') diff --git a/base/src/main/java/bjc/utils/exceptions/BadPragmaFormat.java b/base/src/main/java/bjc/utils/exceptions/BadPragmaFormat.java new file mode 100644 index 0000000..48b5be0 --- /dev/null +++ b/base/src/main/java/bjc/utils/exceptions/BadPragmaFormat.java @@ -0,0 +1,28 @@ +package bjc.utils.exceptions; + +import java.util.InputMismatchException; + +/** + * The exception to throw whenever a pragma is used with invalid syntax. + * + * @author ben + */ +public class BadPragmaFormat extends InputMismatchException { + /* Version ID for serialization. */ + private static final long serialVersionUID = 1288536477368021069L; + + /** Create a new exception */ + public BadPragmaFormat() { + super(); + } + + /** + * Create a new exception with the given message. + * + * @param message + * The message to explain why the exception was thrown. + */ + public BadPragmaFormat(final String message) { + super(message); + } +} diff --git a/base/src/main/java/bjc/utils/exceptions/DirectionInvalid.java b/base/src/main/java/bjc/utils/exceptions/DirectionInvalid.java new file mode 100644 index 0000000..25f6cf8 --- /dev/null +++ b/base/src/main/java/bjc/utils/exceptions/DirectionInvalid.java @@ -0,0 +1,26 @@ +package bjc.utils.exceptions; + +/** + * Represents the condition where a direction has been used in a improper manner + * + * @author ben + * + */ +public class DirectionInvalid extends IllegalArgumentException { + + /** + * Version for serialization + */ + private static final long serialVersionUID = 6852151917518831932L; + + /** + * Create a new {@link DirectionInvalid} with the given cause + * + * @param cause + * The situation that resulting in this exit being thrown + */ + public DirectionInvalid(String cause) { + super(cause); + } + +} diff --git a/base/src/main/java/bjc/utils/exceptions/FileNotChosenException.java b/base/src/main/java/bjc/utils/exceptions/FileNotChosenException.java deleted file mode 100644 index dcd47de..0000000 --- a/base/src/main/java/bjc/utils/exceptions/FileNotChosenException.java +++ /dev/null @@ -1,28 +0,0 @@ -package bjc.utils.exceptions; - -import java.io.IOException; - -/** - * Represents the user failing to choose a file. - * - * @author ben - */ -public class FileNotChosenException extends IOException { - /* Version ID for serialization. */ - private static final long serialVersionUID = -8753348705210831096L; - - /** Create a new exception. */ - public FileNotChosenException() { - super(); - } - - /** - * Create a new exception with the given cause. - * - * @param cause - * The cause of why the exception was thrown. - */ - public FileNotChosenException(final String cause) { - super(cause); - } -} diff --git a/base/src/main/java/bjc/utils/exceptions/InvalidDirectionException.java b/base/src/main/java/bjc/utils/exceptions/InvalidDirectionException.java deleted file mode 100644 index d20b2d1..0000000 --- a/base/src/main/java/bjc/utils/exceptions/InvalidDirectionException.java +++ /dev/null @@ -1,26 +0,0 @@ -package bjc.utils.exceptions; - -/** - * Represents the condition where a direction has been used in a improper manner - * - * @author ben - * - */ -public class InvalidDirectionException extends IllegalArgumentException { - - /** - * Version for serialization - */ - private static final long serialVersionUID = 6852151917518831932L; - - /** - * Create a new {@link InvalidDirectionException} with the given cause - * - * @param cause - * The situation that resulting in this exit being thrown - */ - public InvalidDirectionException(String cause) { - super(cause); - } - -} diff --git a/base/src/main/java/bjc/utils/exceptions/NoFileChosen.java b/base/src/main/java/bjc/utils/exceptions/NoFileChosen.java new file mode 100644 index 0000000..c627dab --- /dev/null +++ b/base/src/main/java/bjc/utils/exceptions/NoFileChosen.java @@ -0,0 +1,28 @@ +package bjc.utils.exceptions; + +import java.io.IOException; + +/** + * Represents the user failing to choose a file. + * + * @author ben + */ +public class NoFileChosen extends IOException { + /* Version ID for serialization. */ + private static final long serialVersionUID = -8753348705210831096L; + + /** Create a new exception. */ + public NoFileChosen() { + super(); + } + + /** + * Create a new exception with the given cause. + * + * @param cause + * The cause of why the exception was thrown. + */ + public NoFileChosen(final String cause) { + super(cause); + } +} diff --git a/base/src/main/java/bjc/utils/exceptions/PragmaFormatException.java b/base/src/main/java/bjc/utils/exceptions/PragmaFormatException.java deleted file mode 100644 index 684c63d..0000000 --- a/base/src/main/java/bjc/utils/exceptions/PragmaFormatException.java +++ /dev/null @@ -1,28 +0,0 @@ -package bjc.utils.exceptions; - -import java.util.InputMismatchException; - -/** - * The exception to throw whenever a pragma is used with invalid syntax. - * - * @author ben - */ -public class PragmaFormatException extends InputMismatchException { - /* Version ID for serialization. */ - private static final long serialVersionUID = 1288536477368021069L; - - /** Create a new exception */ - public PragmaFormatException() { - super(); - } - - /** - * Create a new exception with the given message. - * - * @param message - * The message to explain why the exception was thrown. - */ - public PragmaFormatException(final String message) { - super(message); - } -} diff --git a/base/src/main/java/bjc/utils/exceptions/UnknownPragma.java b/base/src/main/java/bjc/utils/exceptions/UnknownPragma.java new file mode 100644 index 0000000..0f2f7c4 --- /dev/null +++ b/base/src/main/java/bjc/utils/exceptions/UnknownPragma.java @@ -0,0 +1,24 @@ +package bjc.utils.exceptions; + +import java.util.InputMismatchException; + +/** + * Represents a error from encountering a unknown pragma. + * + * @author ben + */ +public class UnknownPragma extends InputMismatchException { + /* Version ID for serialization. */ + private static final long serialVersionUID = -4277573484926638662L; + + /** + * Create a new exception with the given cause. + * + * @param cause + * The cause for throwing this exception. + */ + public UnknownPragma(final String cause) { + super(cause); + } + +} diff --git a/base/src/main/java/bjc/utils/exceptions/UnknownPragmaException.java b/base/src/main/java/bjc/utils/exceptions/UnknownPragmaException.java deleted file mode 100644 index dec89a3..0000000 --- a/base/src/main/java/bjc/utils/exceptions/UnknownPragmaException.java +++ /dev/null @@ -1,24 +0,0 @@ -package bjc.utils.exceptions; - -import java.util.InputMismatchException; - -/** - * Represents a error from encountering a unknown pragma. - * - * @author ben - */ -public class UnknownPragmaException extends InputMismatchException { - /* Version ID for serialization. */ - private static final long serialVersionUID = -4277573484926638662L; - - /** - * Create a new exception with the given cause. - * - * @param cause - * The cause for throwing this exception. - */ - public UnknownPragmaException(final String cause) { - super(cause); - } - -} diff --git a/base/src/main/java/bjc/utils/gui/SimpleFileChooser.java b/base/src/main/java/bjc/utils/gui/SimpleFileChooser.java index 7be9f84..d6ae97b 100644 --- a/base/src/main/java/bjc/utils/gui/SimpleFileChooser.java +++ b/base/src/main/java/bjc/utils/gui/SimpleFileChooser.java @@ -5,7 +5,7 @@ import java.io.File; import javax.swing.JFileChooser; -import bjc.utils.exceptions.FileNotChosenException; +import bjc.utils.exceptions.NoFileChosen; /** * Utility class for easily prompting user for files. @@ -30,7 +30,7 @@ public class SimpleFileChooser { maybeDoOpenFile(parent, files); success = true; - } catch (final FileNotChosenException fncx) { + } catch (final NoFileChosen fncx) { // We don't care about specifics SimpleDialogs.showError(parent, "I/O Error", "Please pick a file to open"); @@ -54,7 +54,7 @@ public class SimpleFileChooser { maybeDoSaveFile(parent, files); return files.getSelectedFile(); - } catch (final FileNotChosenException fncex) { + } catch (final NoFileChosen fncex) { // We don't care about specifics SimpleDialogs.showError(parent, "I/O Error", "Please pick a file to save to"); @@ -135,7 +135,7 @@ public class SimpleFileChooser { } private static void maybeDoOpenFile(final Component parent, final JFileChooser files) - throws FileNotChosenException { + throws NoFileChosen { if (parent == null) throw new NullPointerException("Parent must not be null"); else if (files == null) @@ -144,11 +144,11 @@ public class SimpleFileChooser { final int result = files.showSaveDialog(parent); if (result != JFileChooser.APPROVE_OPTION) - throw new FileNotChosenException(); + throw new NoFileChosen(); } private static void maybeDoSaveFile(final Component parent, final JFileChooser files) - throws FileNotChosenException { + throws NoFileChosen { if (parent == null) throw new NullPointerException("Parent must not be null"); else if (files == null) @@ -157,7 +157,7 @@ public class SimpleFileChooser { final int result = files.showSaveDialog(parent); if (result != JFileChooser.APPROVE_OPTION) - throw new FileNotChosenException(); + throw new NoFileChosen(); } /** @@ -178,7 +178,7 @@ public class SimpleFileChooser { try { maybeDoOpenFile(parent, files); - } catch (final FileNotChosenException fncex) { + } catch (final NoFileChosen fncex) { // We don't care about specifics } @@ -203,7 +203,7 @@ public class SimpleFileChooser { try { maybeDoSaveFile(parent, files); - } catch (final FileNotChosenException fncex) { + } catch (final NoFileChosen fncex) { // We don't care about specifics } diff --git a/base/src/main/java/bjc/utils/ioutils/RuleBasedConfigReader.java b/base/src/main/java/bjc/utils/ioutils/RuleBasedConfigReader.java index fe303f3..2ce2591 100644 --- a/base/src/main/java/bjc/utils/ioutils/RuleBasedConfigReader.java +++ b/base/src/main/java/bjc/utils/ioutils/RuleBasedConfigReader.java @@ -10,7 +10,7 @@ import bjc.data.IHolder; import bjc.data.IPair; import bjc.data.Identity; import bjc.data.Pair; -import bjc.utils.exceptions.UnknownPragmaException; +import bjc.utils.exceptions.UnknownPragma; import bjc.funcdata.FunctionalMap; import bjc.funcdata.FunctionalStringTokenizer; import bjc.funcdata.IMap; @@ -271,7 +271,7 @@ public class RuleBasedConfigReader { * Handle pragmas */ pragmas.getOrDefault(token, (tokenzer, stat) -> { - throw new UnknownPragmaException("Unknown pragma " + token); + throw new UnknownPragma("Unknown pragma " + token); }).accept(tokenizer, state); } else { /* diff --git a/base/src/main/java/bjc/utils/ioutils/RuleBasedReaderPragmas.java b/base/src/main/java/bjc/utils/ioutils/RuleBasedReaderPragmas.java index 42a01a4..535b3d4 100644 --- a/base/src/main/java/bjc/utils/ioutils/RuleBasedReaderPragmas.java +++ b/base/src/main/java/bjc/utils/ioutils/RuleBasedReaderPragmas.java @@ -2,7 +2,7 @@ package bjc.utils.ioutils; import java.util.function.BiConsumer; -import bjc.utils.exceptions.PragmaFormatException; +import bjc.utils.exceptions.BadPragmaFormat; import bjc.funcdata.FunctionalStringTokenizer; import bjc.utils.funcutils.ListUtils; @@ -35,7 +35,7 @@ public class RuleBasedReaderPragmas { if (!tokenizer.hasMoreTokens()) { String fmt = "Pragma %s requires one integer argument"; - throw new PragmaFormatException(String.format(fmt, name)); + throw new BadPragmaFormat(String.format(fmt, name)); } /* @@ -55,8 +55,8 @@ public class RuleBasedReaderPragmas { String fmt = "Argument %s to %s pragma isn't a valid integer, and this pragma requires an integer argument."; - final PragmaFormatException pfex - = new PragmaFormatException(String.format(fmt, token, name)); + final BadPragmaFormat pfex + = new BadPragmaFormat(String.format(fmt, token, name)); pfex.initCause(nfex); @@ -87,7 +87,7 @@ public class RuleBasedReaderPragmas { if (!tokenizer.hasMoreTokens()) { String fmt = "Pragma %s requires one or more string arguments."; - throw new PragmaFormatException(String.format(fmt, name)); + throw new BadPragmaFormat(String.format(fmt, name)); } /* diff --git a/base/src/main/java/bjc/utils/misc/Direction.java b/base/src/main/java/bjc/utils/misc/Direction.java index 6455d8d..cc24b49 100644 --- a/base/src/main/java/bjc/utils/misc/Direction.java +++ b/base/src/main/java/bjc/utils/misc/Direction.java @@ -5,7 +5,7 @@ import java.util.function.Consumer; import org.apache.commons.lang3.text.WordUtils; -import bjc.utils.exceptions.InvalidDirectionException; +import bjc.utils.exceptions.DirectionInvalid; import bjc.funcdata.FunctionalList; import bjc.funcdata.IList; @@ -122,7 +122,7 @@ public enum Direction { case UP: return false; default: - throw new InvalidDirectionException( + throw new DirectionInvalid( "WAT. Somehow ended up with an invalid direction " + this); } } @@ -171,7 +171,7 @@ public enum Direction { case UP: case DOWN: default: - throw new InvalidDirectionException( + throw new DirectionInvalid( "Can't rotate non-cardinal direction clockwise: " + this); } @@ -197,7 +197,7 @@ public enum Direction { case UP: case DOWN: default: - throw new InvalidDirectionException( + throw new DirectionInvalid( "Can't rotate non-cardinal direction counterclockwise: " + this); } diff --git a/base/src/main/java/bjc/utils/misc/RelativeDirection.java b/base/src/main/java/bjc/utils/misc/RelativeDirection.java index 7012377..4239b0d 100644 --- a/base/src/main/java/bjc/utils/misc/RelativeDirection.java +++ b/base/src/main/java/bjc/utils/misc/RelativeDirection.java @@ -3,7 +3,7 @@ package bjc.utils.misc; import java.util.Random; import java.util.function.Consumer; -import bjc.utils.exceptions.InvalidDirectionException; +import bjc.utils.exceptions.DirectionInvalid; import bjc.funcdata.FunctionalList; import bjc.funcdata.IList; @@ -108,7 +108,7 @@ public enum RelativeDirection { case RIGHT: return dir.rotateClockwise(); default: - throw new InvalidDirectionException( + throw new DirectionInvalid( "Attempted to make absolute a direction in a unknown way " + this); } -- cgit v1.2.3