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 OverflowHow do you create a method to throw an exception in java? - Stack Overflow
Should I throw an exception or return a boolean?
Throwing exceptions for "workflow" is generally a bad idea. I like seppyk's suggestion of separating into either multiple methods or renaming the purpose of the method.
There are some technical (that I can't explain well) reasons why using exceptions like this is a bad idea. Something to do with the JIT compiler reordering your bytecode for efficiency, but when the exception is thrown it's forced to figure out your method stack and it goes from being more efficient to less efficient because you're throwing exceptions in non-exceptional cases. Do some googling if you want a better explanation on that one.
Aside from the technical reasons why this is a bad idea, you're effectively using a GOTO statement in the form of an exception. I've done this before when adding to old code and I didn't have the luxury of being able to refactor everything, but it's a hack and should be treated as such.
More on reddit.com