Your current method covers the first point - to take an exception as parameter.
The throws Exception is unnecessary if you dont throw it from a method, but it is needed to cover the second point.

As commenters pointed, you just need to use the throw keyword to throw an exception, so the method could look like:

public void throwException (Exception ex) throws Exception {
    //some other code maybe?
    throw ex;
}

This implementation has a little flaw. When the null is passed as a parameter to it, the method will throw a NullPointerException, because the throw keyword accepts objects of the Throwable class or its subclasses (Exception is a subclass of Throwable).
To avoid NullPointerException (which is an unchecked-exception), simple if statement can be used:

public void throwException (Exception ex) throws Exception {
    if (ex != null) {
        throw ex;
    }
    //just for presentation,below it throws new Exception
    throw new Exception("ex parameter was null");
}

Edit:
As @Slaw suggested, in that very small case adding the null-check and throwing new Exception just disguises the NullPointerException. Without the null-check and throw new... the NPE will be thrown from that method and its stacktrace will show exact line of throw ex when the null is passed to that method.
The NPE is a subtype of RuntimeException class and the subtypes of RuntimeException doesn't need to be explicitly declared in method signature when they are thrown from that method. Like here:

public static void throwNPE(Exception e) {
    throw new NullPointerException();
}

The RuntimeException and its subclasses are called an unchecked-exceptions. Other classes extending one of Exception or Throwable classes are call checked-exceptions, because if a method throws them, it must declare that exception (or superclass) in the signature or explicitly try-catch it.

The proper use of null-check would be when the method would throw a more specific kind of exception (like IOException or a new subclass of Exception/Throwable) and the when the other method using the one which throws that new type of Exception would try-catch that specific type the NPE wouldn't be caught.

Just for good practices, when dealing with try-catch it's much better to catch exact types of thrown Exceptions instead of general Exception/Throwable. It helps to understand the real cause of exception, debug and fix code.

Answer from itwasntme on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › exceptions › throwing.html
How to Throw Exceptions (The Java™ Tutorials > Essential Java Classes > Exceptions)
The pop method checks to see whether any elements are on the stack. If the stack is empty (its size is equal to 0), pop instantiates a new EmptyStackException object (a member of java.util) and throws it. The Creating Exception Classes section in this chapter explains how to create your own exception classes.
🌐
GeeksforGeeks
geeksforgeeks.org › java › throw-throws-java
throw and throws in Java - GeeksforGeeks
The throw keyword in Java is used to explicitly throw an exception from a method or any block of code. We can throw either checked or unchecked exception. The throw keyword is mainly used to throw custom exceptions.
Published   August 5, 2025
🌐
Rollbar
rollbar.com › home › how to throw exceptions in java
How to Throw Exceptions in Java | Rollbar
2 weeks ago - The detailMessage parameter gives the details of the message for this exception, and the throwable parameter gives the cause of this exception. The key takeaway: every exception needs a message explaining what went wrong, and optionally a cause pointing to the underlying exception that triggered it. Java’s built-in exceptions don’t always provide the information we need.
🌐
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 - Another example is NullPointerException, which may be thrown when a variable that is not pointing to any object (and refers to nothing, or null) is accessed. In Java, there are two types of exceptions: checked and unchecked.
🌐
W3Schools
w3schools.com › java › java_try_catch.asp
Java Exceptions (Try...Catch)
The throw statement is used together with an exception type. There are many exception types available in Java: ArithmeticException, FileNotFoundException, ArrayIndexOutOfBoundsException, SecurityException, etc:
🌐
DataCamp
datacamp.com › doc › java › throw
throw Keyword in Java: Usage & Examples
The throw keyword is typically used to throw either checked or unchecked exceptions. When an exception is thrown, the normal flow of the program is disrupted, and control is transferred to the nearest enclosing try-catch block that can handle the exception. ... ExceptionType: The type of exception ...
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › exceptions › declaring.html
Specifying the Exceptions Thrown by a Method (The Java™ Tutorials > Essential Java Classes > Exceptions)
If the writeList method doesn't catch the checked exceptions that can occur within it, the writeList method must specify that it can throw these exceptions. Let's modify the original writeList method to specify the exceptions it can throw instead of catching them.
🌐
UW Computer Sciences
pages.cs.wisc.edu › ~paton › readings › Old › fall08 › 3.EXCEPTIONS.html
JAVA EXCEPTIONS
public class TestExceptions { static void e() { // might cause any of the following unchecked exceptions to be thrown: // Ex1, Ex2, Ex3, Ex4 } static void d() { try { e(); } catch (Ex1 ex) { System.out.println("d caught Ex1"); } } static void c() { try { d(); } catch (Ex2 ex) { System.out.println("c caught Ex2"); // now cause exception Ex1 to be thrown } } static void b() { try { c(); } catch (Ex1 ex) { System.out.println("b caught Ex1"); } catch (Ex3 ex) { System.out.println("b caught Ex3"); } } static void a() { try { b(); } catch (Ex4 ex) { System.out.println("a caught Ex4"); // now cause e
Find elsewhere
🌐
W3Schools
w3schools.com › java › ref_keyword_throws.asp
Java throws Keyword
The throws keyword indicates what exception type may be thrown by a method. There are many exception types available in Java: ArithmeticException, ClassNotFoundException, ArrayIndexOutOfBoundsException, SecurityException, etc.
Top answer
1 of 1
3

Your current method covers the first point - to take an exception as parameter.
The throws Exception is unnecessary if you dont throw it from a method, but it is needed to cover the second point.

As commenters pointed, you just need to use the throw keyword to throw an exception, so the method could look like:

public void throwException (Exception ex) throws Exception {
    //some other code maybe?
    throw ex;
}

This implementation has a little flaw. When the null is passed as a parameter to it, the method will throw a NullPointerException, because the throw keyword accepts objects of the Throwable class or its subclasses (Exception is a subclass of Throwable).
To avoid NullPointerException (which is an unchecked-exception), simple if statement can be used:

public void throwException (Exception ex) throws Exception {
    if (ex != null) {
        throw ex;
    }
    //just for presentation,below it throws new Exception
    throw new Exception("ex parameter was null");
}

Edit:
As @Slaw suggested, in that very small case adding the null-check and throwing new Exception just disguises the NullPointerException. Without the null-check and throw new... the NPE will be thrown from that method and its stacktrace will show exact line of throw ex when the null is passed to that method.
The NPE is a subtype of RuntimeException class and the subtypes of RuntimeException doesn't need to be explicitly declared in method signature when they are thrown from that method. Like here:

public static void throwNPE(Exception e) {
    throw new NullPointerException();
}

The RuntimeException and its subclasses are called an unchecked-exceptions. Other classes extending one of Exception or Throwable classes are call checked-exceptions, because if a method throws them, it must declare that exception (or superclass) in the signature or explicitly try-catch it.

The proper use of null-check would be when the method would throw a more specific kind of exception (like IOException or a new subclass of Exception/Throwable) and the when the other method using the one which throws that new type of Exception would try-catch that specific type the NPE wouldn't be caught.

Just for good practices, when dealing with try-catch it's much better to catch exact types of thrown Exceptions instead of general Exception/Throwable. It helps to understand the real cause of exception, debug and fix code.

🌐
Medium
medium.com › @AlexanderObregon › java-exception-handling-throws-vs-try-catch-94b0abe1080d
Java Exception Handling — Throws vs. Try-Catch
March 17, 2024 - Document Rethrown Exceptions: Clearly document any exceptions that your method rethrows, especially if they change the type or add context. This documentation is important for other developers who use your method. Combining throws and try-catch offers Java developers a nuanced approach to exception handling.
🌐
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.
🌐
Stackify
stackify.com › types-of-exceptions-java
Types of Exceptions in Java - Stackify
March 14, 2024 - Then, the IllegalArgumentException is thrown to indicate the user that you cannot pass a null input string to the method. Consider the following code snippet to demonstrate this type of exception: import java.io.File; public class Sample_IllegalArgumentException { public static String createRelativePath(String par, String f_name) { if (par == null) throw new IllegalArgumentException("You cannot provide null parent path!"); if (f_name == null) throw new IllegalArgumentException("Please enter the complete filename!"); return par + File.separator + f_name; } public static void main(String[] args) { // This command will be successfully executed.
🌐
Tutorialspoint
tutorialspoint.com › java › java_exceptions.htm
Java - Exceptions
Try to understand the difference between throws and throw keywords, throws is used to postpone the handling of a checked exception and throw is used to invoke an exception explicitly. The following method declares that it throws a RemoteException − · import java.io.*; public class className ...
🌐
Tutorialspoint
tutorialspoint.com › java › java_throw_exception.htm
Java - Throws and Throw | Throw an Exception
A method can declare that it throws more than one exception, in which case the exceptions are declared in a list separated by commas. For example, the following method declares that it throws a RemoteException and an InsufficientFundsException − · import java.io.*; public class className { public void withdraw(double amount) throws RemoteException, InsufficientFundsException { // Method implementation } // Remainder of class definition }
🌐
Medium
medium.com › javarevisited › how-to-throw-exceptions-in-java-using-throw-throws-keywords-throwing-exceptions-7082007f6462
How To Throw Exceptions In Java Using throw, throws Keywords | Throwing Exceptions | by Mouad Oumous | Javarevisited | Medium
February 22, 2024 - There are many exception types available in Java: ArithmeticException, ClassNotFoundException, ArrayIndexOutOfBoundsException, SecurityException, etc. ... Here, ThrowableInstance must be an object of type Throwable or a subclass of Throwable.
Top answer
1 of 8
30

You can throw a new IllegalArgumentException().

Thrown to indicate that a method has been passed an illegal or inappropriate argument.

Just don't forget to pass a clear message as a first argument. This will really help you to understand what happend.

For example "Can't use mean on an empty List".

2 of 8
12

The question to ask yourself first is whether you should be throwing at all and then, if so, whether it should be a checked or unchecked exception.

Unfortunately, there's no industry best practice on deciding these things, as shown by this StackOverflow answer:

In Java, when should I create a checked exception, and when should it be a runtime exception?

Nevertheless, there are some key considerations:

  • Your design/vision for how this method is supposed to work (Is it reasonable/normal for the method to be called with 0-size list)?

  • Consistency with other methods in the class/package

  • Compliance with your applicable coding standard (if any)

My opinion:

  • Return Double.NAN or 0 If calling the method with a 0-size list is reasonable/expected/normal, I'd consider returning Double.NAN or 0 if 0 is appropriate for your problem domain.

  • Throw anIllegalArgumentException If my design says that checking for an empty List is strongly the responsibility of the caller and the documentation for the method is going to clearly state that it is the responsibility of the caller, then I'd use the standard unchecked IllegalArgumentException.

  • Throw a custom checked exception If the method is part of a statistics package or library where several statistics functions need to deal with an possible empty data set, I'd think this is an exception condition that is part of the problem domain. I'd create a custom (probably checked) exception (e.g. EmptyDataSetException) to be part of the class/package/library and use it across all applicable methods. Making it a checked exceptions helps remind the client to consider how to handle the condition.

🌐
GeeksforGeeks
geeksforgeeks.org › java › exceptions-in-java
Java Exception Handling - GeeksforGeeks
In Java, all exceptions and errors are subclasses of the Throwable class. It has two main branches ... Java defines several types of exceptions that relate to its various class libraries.
Published   December 23, 2016
Top answer
1 of 9
191

In Java, as you may know, exceptions can be categorized into two: One that needs the throws clause or must be handled if you don't specify one and another one that doesn't. Now, see the following figure:

In Java, you can throw anything that extends the Throwable class. However, you don't need to specify a throws clause for all classes. Specifically, classes that are either an Error or RuntimeException or any of the subclasses of these two. In your case Exception is not a subclass of an Error or RuntimeException. So, it is a checked exception and must be specified in the throws clause, if you don't handle that particular exception. That is why you needed the throws clause.


From Java Tutorial:

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.

Now, as you know exceptions are classified into two: checked and unchecked. Why these classification?

Checked Exception: They are used to represent problems that can be recovered during the execution of the program. They usually are not the programmer's fault. For example, a file specified by user is not readable, or no network connection available, etc., In all these cases, our program doesn't need to exit, instead it can take actions like alerting the user, or go into a fallback mechanism(like offline working when network not available), etc.

Unchecked Exceptions: They again can be divided into two: Errors and RuntimeExceptions. One reason for them to be unchecked is that they are numerous in number, and required to handle all of them will clutter our program and reduce its clarity. The other reason is:

  • Runtime Exceptions: They usually happen due to a fault by the programmer. For example, if an ArithmeticException of division by zero occurs or an ArrayIndexOutOfBoundsException occurs, it is because we are not careful enough in our coding. They happen usually because some errors in our program logic. So, they must be cleared before our program enters into production mode. They are unchecked in the sense that, our program must fail when it occurs, so that we programmers can resolve it at the time of development and testing itself.

  • Errors: Errors are situations from which usually the program cannot recover. For example, if a StackOverflowError occurs, our program cannot do much, such as increase the size of program's function calling stack. Or if an OutOfMemoryError occurs, we cannot do much to increase the amount of RAM available to our program. In such cases, it is better to exit the program. That is why they are made unchecked.

For detailed information see:

  • Unchecked Exceptions — The Controversy
  • The Catch or Specify Requirement
2 of 9
35

Java requires that you handle or declare all exceptions. If you are not handling an Exception using a try/catch block then it must be declared in the method's signature.

For example:

class throwseg1 {
    void show() throws Exception {
        throw new Exception();
    }
}

Should be written as:

class throwseg1 {
    void show() {
        try {
            throw new Exception();
        } catch(Exception e) {
            // code to handle the exception
        }
    }
}

This way you can get rid of the "throws Exception" declaration in the method declaration.

🌐
Florida State University
cs.fsu.edu › ~myers › cop3331 › notes › javaexcept.html
Exception Handling in Java
In Java, exception objects are all children of class Throwable. This base class has two main child classes: class Exception: base class for most exception types