UnsupportedAddressTypeException is a subclass of RuntimeException, and from the JavaDoc:

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.

A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.

Answer from Jonathan on Stack Overflow
๐ŸŒ
Javatpoint
javatpoint.com โ€บ custom-exception
Java Custom Exception - javatpoint
Java Custom Exception. If you are creating your own Exception that is known as custom exception or user-defined exception.Let's see example of custom exception.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ user-defined-custom-exception-in-java
User-Defined Custom Exception in Java - GeeksforGeeks
There are two types of custom exceptions in Java. Checked Exceptions: It extends the Exception class. and it must be declared in the throws clause of the method signature.
Published ย  August 14, 2025
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ core java โ€บ create a custom exception in java
Create a Custom Exception in Java | Baeldung
May 11, 2024 - try (Scanner file = new Scanner(new ... the root cause of the exception. To fix this, we can also add a java.lang.Throwable parameter to the constructor....
๐ŸŒ
Alvin Alexander
alvinalexander.com โ€บ java โ€บ java-custom-exception-create-throw-exception
Java: How to create and throw a custom exception | alvinalexander.com
As you can see, all you need to do to throw your custom exception is (1) create a new instance of the exception (new AlsCustomException("Anything but zero ...")), and then (2) throw that exception with the throw keyword. With those two pieces in place, we'll create a "driver" class with a main ...
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ java_custom_exception.htm
Java - Custom Exception
Here is the syntax to create a custom class in Java - ... You just need to extend the predefined Exception class to create your own Exception. These are considered to be checked exceptions. Keep the following points in mind when writing your own exception classes โˆ’ ยท All exceptions must ...
๐ŸŒ
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 - By making information about potential exceptions available, any caller can decide what to do with the exception: ignore it by adding a throws to the method signature or catch the exception and handle it.
๐ŸŒ
Tpoint Tech
tpointtech.com โ€บ custom-exception
Custom (User-Defined) Exception in Java - Tpoint Tech
In Java, we can create our exceptions that handle specific application requirements. Custom exceptions are derived classes of the Exception class. Custom exc...
Find elsewhere
Top answer
1 of 9
274

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
}
2 of 9
131

You need to create a class that extends from Exception. It should look like this:

public class MyOwnException extends Exception {
    public MyOwnException () {

    }

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

    public MyOwnException (Throwable cause) {
        super (cause);
    }

    public MyOwnException (String message, Throwable cause) {
        super (message, cause);
    }
}

Your question does not specify if this new exception should be checked or unchecked.

As you can see here, the two types are different:

  • Checked exceptions are meant to flag a problematic situation that should be handled by the developer who calls your method. It should be possible to recover from such an exception. A good example of this is a FileNotFoundException. Those exceptions are subclasses of Exception.

  • Unchecked exceptions are meant to represent a bug in your code, an unexpected situation that you might not be able to recover from. A NullPointerException is a classical example. Those exceptions are subclasses of RuntimeException

Checked exception must be handled by the calling method, either by catching it and acting accordingly, or by throwing it to the calling method. Unchecked exceptions are not meant to be caught, even though it is possible to do so.

๐ŸŒ
Javatpoint
javatpoint.com โ€บ throw-keyword
Java Throw Keyword - javatpoint
Java Throw exception. The Java throw keyword is used to explicitely throw an exception. Let's see its example.
๐ŸŒ
Rollbar
rollbar.com โ€บ home โ€บ how to throw exceptions in java
How to Throw Exceptions in Java | Rollbar
2 weeks ago - You now have the tools to throw, declare, and create custom exceptions in your Java applications.
๐ŸŒ
Medium
medium.com โ€บ @salvipriya97 โ€บ custom-exceptions-in-java-301ef3b568a3
Custom Exceptions in Java. What is a Custom Exception? | by Priya Salvi | Medium
February 13, 2024 - A custom exception in Java is essentially a user-defined exception class that extends either Exception (for checked exceptions) or RuntimeException (for unchecked exceptions).
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2013 โ€บ 04 โ€บ user-defined-exception-in-java
User defined exception in java
*/ MyException(String str2) { str1=str2; } public String toString(){ return ("MyException Occurred: "+str1) ; } } class Example1{ public static void main(String args[]){ try{ System.out.println("Starting of try block"); // I'm throwing the custom exception using throw throw new MyException("This ...
Top answer
1 of 4
4

Java provides 2 types of exceptions : Checked and Unchecked.

Checked once are the those which the compiler will prompt you too handle (in a try-catch block) while for the unchecked exceptions compiler will not ask you to handle them.

RuntimeException class is the top level class for Unchecked exceptions and hence if your custom exception classes extends RuntimeException class then automatically compiler will not prompt you to handle them explicitly in a try-catch block.

Basically RuntimeExceptions are those which java considers as occurring at the execution time and anything occurring at the execution time like NullPointerException cannot be handled, hence we cannot do much at the compile time.

2 of 4
2

If you don't want to force your code to wrap these methods in try blocks, why are you having them throw exceptions?

Let's consider these methods:

public void badMethodOne() throws CustomExceptionOne;
public void badMethodTwo() throws CustomExceptionTwo;

You're asking how you can avoid this scenario:

public void myMethod(){
   try{
      badMethodOne();
   }
   catch(CustomExceptionOne ex){
      ex.printStackTrace();
   }

   try{
      badMethodTwo();
   }
   catch(CustomExceptionTwo ex){
      ex.printStackTrace();
   }

}

One option would be to simply catch a superclass Exception:

public void myMethod(){
   try{
      badMethodOne();
      badMethodTwo();
   }
   catch(Exception ex){
      ex.printStackTrace();
   }

}

You might also use a multi-catch block:

public void myMethod(){
   try{
      badMethodOne();
      badMethodTwo();
   }
   catch(CustomExceptionOne | CustomExceptionTwo ex){
      ex.printStackTrace();
   }
}

You might also pass responsibility "up the chain" and let whoever's calling your code handle the try/catch:

public void myMethod() throws CustomExceptionOne, CustomExceptionTwo{
      badMethodOne();
      badMethodTwo();
}
๐ŸŒ
CodeJava
codejava.net โ€บ java-core โ€บ exception โ€บ how-to-create-custom-exceptions-in-java
How to create custom exceptions in Java
Itโ€™s a common practice for catching a built-in exception and re-throwing it via a custom exception. To do so, let add a new constructor to our custom exception class. This constructor takes two parameters: the detail message and the cause of the exception.
๐ŸŒ
Reddit
reddit.com โ€บ r/javahelp โ€บ help with throwing custom exception (homework)
r/javahelp on Reddit: Help with throwing custom exception (Homework)
January 29, 2021 -

I don't really work with exception handling a lot, so I'm out of my comfort zone for this assignment. In essence, the code I'm testing is a List object, checking it to see if the object I want to add already exists within the list, and throwing a custom exception if it does. I'm using junit testing, if that matters.

public void addConnection(LinkedInUser user) throws LinkedInException
{
	try {
		for (int x = 0; x < connections.size(); x++)
		{
			if (connections.get(x) == user)
				throw new LinkedInException();
		}
		//if (connections.contains(user))
		//	throw new LinkedInException();
		//else
		connections.add(user);
	} catch (LinkedInException e) {
		e.Exception("You are already connected with this user");
	}
}

The commented portion was the old code. I think the problem is determining whether or not the object is already within the list, but I could be wrong. The LinkedInException class is just an empty class. Our instructor told us to override the five Exception class constructors, but that was it, so the overrides are blank.

Here's the junit test code

@Test
public void errorCheckAddCon()
{
	LinkedInUser test = new LinkedInUser("han", "password123");
	LinkedInUser test1 = new LinkedInUser("luke", "password123");
	boolean exceptionThrown = false;
	
	try {
		test.addConnection(test1);
		test.addConnection(test1);
	} catch (LinkedInException e) {
		exceptionThrown = true;
	}
	
	Assert.assertTrue(exceptionThrown);
}

(I think it's the addConnection method, and specifically the List.contains method within, because I manually set boolean exceptionThrown to true, and the junit test passed, so the junit test code isn't the issue here I believe)

Top answer
1 of 2
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2 of 2
1
Why do you have a try-catch block in the addConnection method? Like what purpose do you think it serves?
๐ŸŒ
LambdaTest Community
community.lambdatest.com โ€บ general discussions
How to Create and Handle a Custom Exception in Java - LambdaTest Community
March 16, 2025 - What is the correct way to create a Java custom exception? I want to define my own exception type and throw it when a specific condition is met. For example, in a try block, I read a string using reader.readLine(), and โ€ฆ
๐ŸŒ
Medium
medium.com โ€บ @nweligalla โ€บ creating-custom-exceptions-in-java-ea77a61fcaf4
Creating Custom Exceptions in Java | by Nayana Weligalla | Medium
January 31, 2024 - This amount is sent to the parent classโ€™s constructor with a concatenated string using super(). Because youโ€™re overriding the default Java constructor with one that takes an amount parameter, you can also create another no-args constructor and use it to throw an exception directly. if you want you can call the super() without any parameters. You donโ€™t have to only extend the Exception class; you can also extend the RuntimeException for your custom exception.
๐ŸŒ
Stackify
stackify.com โ€บ java-custom-exceptions
Implement Custom Exceptions in Java: Why, When and How
May 1, 2023 - /** * The MyBusinessException wraps all checked standard Java exception and enriches them with a custom error code. * You can use this code to retrieve localized error messages and to link to our online documentation. * * @author TJanssen */ public class MyBusinessException extends Exception { ... } Quite often, your code catches a standard exception before you throw your custom exception.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ how-to-make-custom-exceptions-in-java
How to Make Custom Exceptions in Java
November 10, 2019 - In this article, we'll cover the process of creating custom both checked and unchecked exceptions in Java. If you'd like to read more about exceptions and exc...