You should be able to create a custom exception class that extends the Exception
class, for example:
class WordContainsException extends Exception { // Parameterless Constructor public WordContainsException() {} // Constructor that accepts a message public WordContainsException(String message) { super(message); } }
Usage:
try { if(word.contains(" ")) { throw new WordContainsException(); } } catch(WordContainsException ex) { // Process message however you would like }
Related Posts:
- What is a StackOverflowError?
- What is the proper way to handle a NumberFormatException when it is expected?
- Exception in thread “main” java.util.NoSuchElementException
- JavaFX – Exception in Application start method?
- JavaFX – Exception in Application start method? [duplicate]
- Given final block not properly padded
- What is an AssertionError? In which case should I throw it from my own code?
- EOFException – how to handle?
- Can I catch multiple Java exceptions in the same catch clause?
- Exception in thread “main” java.util.NoSuchElementException: No line found
- “NoClassDefFoundError: Could not initialize class” error
- unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
- Exception in thread “main” java.lang.UnsupportedClassVersionError: a (Unsupported major.minor version 51.0)
- “NoClassDefFoundError: Could not initialize class” error
- “NoClassDefFoundError: Could not initialize class” error
- Why do I get an UnsupportedOperationException when trying to remove an element from a List?
- Why do I get “Exception; must be caught or declared to be thrown” when I try to compile my Java code?
- What is difference between Errors and Exceptions?
- Causes of getting a java.lang.VerifyError
- Exception in thread “main” java.lang.ArithmeticException: / by zero
- Java can’t find file when running through Eclipse
- FXML Load exception
- StringIndexOutOfBoundsException String index out of range: 0
- java.lang.ClassNotFoundException:com.mysql.jdbc.Driver [duplicate]
- Connection Java – MySQL : Public Key Retrieval is not allowed
- Why is a ConcurrentModificationException thrown and how to debug it
- Throwing multiple exceptions in Java
- java.lang.ArrayIndexOutOfBoundsException: 0
- Official reasons for “Software caused connection abort: socket write error”
- How can I read a text file in Android?
- Exception is never thrown in body of corresponding try statement
- How can I catch all the exceptions that will be thrown through reading and writing a file?
- javac is not recognized as an internal or external command, operable program or batch file [closed]
- How to uninstall Eclipse?
- How do I determine whether an array contains a particular value in Java?
- How can I solve Exception in thread “main” java.lang.NullPointerException error [duplicate]
- Why am I getting a NoClassDefFoundError in Java?
- How to avoid “ConcurrentModificationException” while removing elements from `ArrayList` while iterating it? [duplicate]
- Using Pairs or 2-tuples in Java [duplicate]
- Error occurred during initialization of boot layer FindException: Module not found
- Java: Multiple class declarations in one file
- Why do JVM arguments start with “-D”?
- How to split a string in Java
- Printing out a linked list using toString
- Received fatal alert: handshake_failure through SSLHandshakeException
- What’s the difference between next() and nextLine() methods from Scanner class?
- How to fix java.net.SocketException: Broken pipe?
- How to identify object types in java
- What is the result of making log4j additivity equals to true or false?
- Attempt to invoke virtual method ‘…’ on a null object reference
- Is there a Mutex in Java?
- maven error: package org.junit does not exist
- ClassNotFoundException: org.slf4j.LoggerFactory
- Problems with setting the classpath in ant
- Spring Data JPA Update @Query not updating?
- What does ‘SSSXXX’ mean in a java simple date format object?
- Why am I getting a “; expected” error?
- How/where to download openjdk/openjre for windows [duplicate]
- copy a 2d array in java
- Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException
- Generating all permutations of a given string
- imageio.IIOException: Can’t read input file
- Difference between OpenJDK and Adoptium/AdoptOpenJDK
- Compile failed; see the compiler error output for details
- Java: Rotating Images
- How to fix the Hibernate “object references an unsaved transient instance – save the transient instance before flushing” error
- channel 3: open failed: connect failed: Connection refused
- Java Currency Number format
- How to create a generic array in Java?
- Print a boolean value with printf
- Meaning of @Resource annotation
- How to solve error lossy convertion from int to char?
- What does “error: ‘.class’ expected” mean and how do I fix it
- Printing the stack values in Java
- Import a custom class in Java
- Java: How to set Precision for double value?
- Any way to declare an array in-line?
- java.io.StreamCorruptedException: invalid stream header: 54657374
- How to cast ArrayList<> from List<>
- Best way to “negate” an instanceof
- what is the difference between doGet() and doPost() in term of the flow?
- Java 3D Game Engine?
- How to cast ArrayList<> from List<>
- Using insertion sort for descending order?
- What is the home directory for JDK?
- java.lang.ClassNotFoundException: org.postgresql.Driver, Android
- Java – Change int to ascii
- Javadoc @author tag good practices
- A fatal error has been detected by the Java Runtime Environment: SIGSEGV, libjvm
- java.lang.IllegalStateException: Failed to introspect Class
- ShoppingCart.Java Program Assignment
- White spaces are required between publicId and systemId
- Http status 401 This request requires HTTP authentication (). in tomcat 6
- Get integer value of the current year in Java
- How to make the division of 2 ints produce a float instead of another int?
- Java Console Prompt for ENTER input before moving on [duplicate]
- Can’t load IA 32-bit .dll on a AMD 64-bit platform
- Android Studio 2.2.3: Gradle project sync failed. Basic functionality (e.g. editting, debugging) will not work properly
- How to draw a circle with given X and Y coordinates as the middle spot of the circle?
- Can’t seem to disable Java Automatic Update