Throwing custom exceptions in Java

Your static method should declare

private static void testex(String test) throws MyException

if you want the method to throw it (and not to catch and handle it internally).


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.


If your exception extends java.lang.Exception, you must catch it (or rethrow). If it extends java.lang.RuntimeException, you are not required to do so. You will find that this is true for all standard exceptions as well.

edit Changed the words must not to not required to

Tags:

Java