You could create your own Exception class:

public class InvalidSpeedException extends Exception {

  public InvalidSpeedException(String message){
     super(message);
  }

}

In your code:

throw new InvalidSpeedException("TOO HIGH");
Answer from Fortega on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › exceptions-in-java
Java Exception Handling - GeeksforGeeks
In Java, exception handling is a mechanism to handle runtime errors, allowing the normal flow of a program to continue.
Published   3 weeks ago
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Exception.html
Exception (Java Platform SE 8 )
October 20, 2025 - Constructs a new exception with the specified detail message, cause, suppression enabled or disabled, and writable stack trace enabled or disabled. ... cause - the cause. (A null value is permitted, and indicates that the cause is nonexistent or unknown.) enableSuppression - whether or not suppression is enabled or disabled · writableStackTrace - whether or not the stack trace should be writable ... Java™ Platform Standard Ed.
Discussions

How can I throw a general exception in Java? - Stack Overflow
As you can see in the first class, I have added a comment ("// throw exception") where I would like to throw an exception. Do I have to define my own class for exceptions or is there some general exception class in Java I can use? More on stackoverflow.com
🌐 stackoverflow.com
java - What is a exception error and how do I fix it? - Stack Overflow
https://www.geeksforgeeks.org/checked-vs-unchecked-exceptions-in-java/ More on stackoverflow.com
🌐 stackoverflow.com
How to trash the exception model in Java: the Either type
Another functional library that has Either. https://github.com/palatable/lambda This one has a try method, Either.trying(...) More on reddit.com
🌐 r/java
32
0
February 18, 2019
Exception in server tick loop. PLEASE HELP!!! [Java] : MinecraftHelp
I've had this problem ever since I tried running my Minecraft server on my new PC, I installed Java 17 btw. Version: 1.19.2 Here is the... More on old.reddit.com
🌐 r/MinecraftHelp
🌐
W3Schools
w3schools.com › java › java_try_catch.asp
Java Exceptions (Try...Catch)
As mentioned in the Errors chapter, different types of errors can occur while running a program - such as coding mistakes, invalid input, or unexpected situations. When an error occurs, Java will normally stop and generate an error message. The technical term for this is: Java will throw an exception (throw an error).
🌐
Baeldung
baeldung.com › home › java › core java › exception handling in java
Exception Handling in Java | Baeldung
May 11, 2024 - Ultimately, exceptions are just Java objects with all of them extending from Throwable: ---> Throwable <--- | (checked) | | | | | ---> Exception Error | (checked) (unchecked) | RuntimeException (unchecked) There are three main categories of exceptional conditions: ... Runtime and unchecked exceptions refer to the same thing. We can often use them interchangeably.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › exceptions › index.html
Lesson: Exceptions (The Java™ Tutorials > Essential Java Classes)
See Java Language Changes for a ... or deprecated options for all JDK releases. The Java programming language uses exceptions to handle errors and other exceptional events....
Find elsewhere
🌐
Raygun
raygun.com › blog › java-exceptions-terminology
Java exceptions: Common terminology with examples · Raygun Blog
October 25, 2022 - Java exceptions are events that disrupt the normal execution of the program. The main goal of exceptions is to separate error-handling from regular code. Exceptions might stem from a wide range of problems such as missing resources, coding errors, ...
🌐
Stackademic
blog.stackademic.com › 99-of-java-developers-get-exception-handling-wrong-heres-what-senior-devs-do-instead-0377133e06cd
99% of Java Developers Get Exception Handling Wrong — Here’s What Senior Devs Do Instead | by Habibwahid | Nov, 2025 | Stackademic
3 weeks ago - // ❌ WRONG - Catches absolutely everything try { String result = fetchDataFromAPI(); processData(result); saveToDatabase(result); } catch (Exception e) { System.out.println("Something went wrong"); return null; } ... Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world. ... I have been a Software Engineer for four years and am highly experienced with Java, Spring Boot, Generative AI, Spring AI, React, Next, AWS, Microservice, etc.
🌐
Rollbar
rollbar.com › home › java exceptions hierarchy explained
Java Exceptions Hierarchy Explained | Rollbar
May 15, 2025 - In Java “an event that occurs during the execution of a program that disrupts the normal flow of instructions” is called an exception. This is generally an unexpected or unwanted event which can occur either at compile-time or run-time in ...
🌐
Tutorialspoint
tutorialspoint.com › java › java_exceptions.htm
Java - Exceptions
The exception class is a subclass of the Throwable class. Other than the exception class there is another subclass called Error which is derived from the Throwable class. Errors are abnormal conditions that happen in case of severe failures, these are not handled by the Java programs.
🌐
Sematext
sematext.com › home › blog › exception handling in java: how-to tutorial with examples & best practices
How to Handle Exceptions in Java: Tutorial with Examples
Yoast SEO for WordPress
Yoast SEO is the most complete WordPress SEO plugin. It handles the technical optimization of your site & assists with optimizing your content.
Price   $69.00
🌐
JetBrains
blog.jetbrains.com › idea › 2024 › 03 › easy-hacks-how-to-throw-java-exceptions
Easy Hacks: How to Throw Java Exceptions | The IntelliJ IDEA Blog
March 12, 2024 - Exceptions in Java are used to indicate that an event occurred during the execution of a program and disrupted the normal flow of instructions. When an exception occurs, the Java runtime automatically stops the execution of the current method.
🌐
DigitalOcean
digitalocean.com › community › tutorials › exception-handling-in-java
Exception Handling in Java | DigitalOcean
October 6, 2022 - The code that specifies what to do in specific exception scenarios is called exception handling. Java creates an exception object when an error occurs while executing a statement.
🌐
Florida State University
cs.fsu.edu › ~myers › cop3331 › notes › javaexcept.html
Exception Handling in Java
An exception is an object that represents an error or an exceptional situation that has occurred · Could represent the run-time environment detecting an operation that's impossible to carry out · Could be created by a method in response to a special situation · In Java, exception objects ...
🌐
Rollbar
rollbar.com › home › most common java exceptions
Most Common Java Exceptions | Rollbar
November 28, 2022 - These exceptions must be caught by a try/catch in the code or noted as thrown by the method. For instance, if a program attempts to access a file that is currently unavailable, the method to access the file must either catch or throw a FileNotFoundException. Error - errors are exceptions that happen externally to your Java program.
🌐
ProTech
protechtraining.com › bookshelf › java_fundamentals_tutorial › exceptions
Java Fundamentals Tutorial: Exceptions | ProTech
Exceptions are events that occur ... array access out of bound, etc.). In Java, an exception is an object that wraps an error event that occurred within a method and contains:...
🌐
Sentry
blog.sentry.io › exception-handling-in-java-with-real-examples
Exception Handling in Java (with Real Examples) | Product Blog • Sentry
July 15, 2025 - A Java developer should have the grace to accept errors, the courage to handle exceptions, and the wisdom to know the difference. An exception can be checked or unchecked. In Java, RuntimeException and its subclasses are unchecked, while all other subclasses of Exception are checked.
🌐
Pluralsight
pluralsight.com › blog › software development
How to handle the 10 most common exceptions in Java | Online Courses, Learning Paths, and Certifications - Pluralsight
July 17, 2024 - Because the caller is passing in an invalid value, Spring’s Assert class throws an IllegalArgumentException. To repair it, follow the instructions outlined in the exception message. One rather curious instance of IllegalArgumentException is when the Java runtime version is lower than the compiler version.
🌐
Programiz
programiz.com › java-programming › exception-handling
Java Exception Handling (With Examples)
The exception is caught by the catch block. And, then the finally block is executed. Note: It is a good practice to use the finally block. It is because it can include important cleanup codes like, code that might be accidentally skipped by return, continue or break ... The Java throw keyword is used to explicitly throw a single exception.