danger mouse
danger mouse

Reputation: 1497

Java, using throw without try block

For the following method, is it okay to throw the exception when it isn't inside a 'try' block?

public void setCapacity(x) throws CapacityExceededException {
    if (x > 10) throw new CapacityExceededException(x);
    else this.x = x;
}

Upvotes: 4

Views: 27790

Answers (4)

Stephen C
Stephen C

Reputation: 718698

... is it okay to throw the exception when it isn't inside a 'try' block?

Of course1 it is!.

Indeed ... that is the most common way to throw exceptions.

Indeed 2 ... if we didn't use exceptions like that, they would be (almost) redundant since we could (almost) replace them with "break to label" and a local variable to hold the exception information.


1 - Provided that the exception is unchecked2, or you declared it in the method signature.

2 - That is RuntimeException, Error or descendants of the same.

Upvotes: 0

techPackets
techPackets

Reputation: 4506

Yes it is Ok to throw an exception when it isn't inside a try block. All you have do is declare that your method throws an exception. Otherwise compiler will give an error.

public void setCapacity(x) throws CapacityExceededException {
    if (x > 10) throw new CapacityExceededException(x);
    else this.x = x;
}

You don't even have to do that if your CapacityExceededException extends Runtime Exception.

public void setA(int a) {
            this.a = a;
            if(a<0) throw new NullPointerException();
        }

This code won't give any compiler error. As NPE is a RuntimeException.

When you throw an exception the exception will be propagated to the method which called setCapacity() method. That method has to deal with the exception via try catch or propagate it further up the call stack by rethrowing it.

Upvotes: 7

akhil_mittal
akhil_mittal

Reputation: 24157

Yes it is ok. You simply need to add throws clause for method in case it throws a checked exception. There is no restriction of a try clause for throwing an exception. For example if you have a stack and called pop() method and size is zero you can check it and throw an exception. You may need to catch the exception wherever you plan to invoke this method. Otherwise this uncaught exception will move up the invocation stack and will be finally handled by JVM and it will print stack trace on System.err stream. We can also specify our own handler if we want:

public class CustomUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
  public void uncaughtException(Thread t, Throwable e) {
     // Write the custom logic here
   }
}

You do not even need to mention exception in throws if it is a RunTimException.

Upvotes: 1

Zizouz212
Zizouz212

Reputation: 4998

Just add a throws declaration:

public int getCapacity() throws CapacityExceededException {
    // Something goes here
}

Otherwise, you'll get compiler warnings. This will get the method to throw the exception. However, whenever you execute the method, you will have to use the try block. This is because an exception could be thrown by execution of the method.

try {
    getCapacity()
} catch (CapacityExceededException e) {
    // Do Something
}

Upvotes: 0

Related Questions