How can I catch an exception declared in a throws
clause if I don't write a try
/catch
clause?
When i write a function like this
public List<String> getTrackIds(int limit) throws NotConnectedException, UnauthorizedException {
...
}
it means that the function can throw these exceptions. But isnt there a need to catch it in try/catch block? How will i catch exceptions in this case?
throws
clause in the calling
function. Again, how will i catch these exceptions if i dont write a
try/catch block?A catch-block will catch a thrown exception if and only if: the thrown exception object is the same as the exception object specified by the catch-block. the thrown exception object is the subtype of the exception object specified by the catch-block.
We can declare both types of exceptions using throws clause i.e. checked and unchecked exceptions. But the method calling the given method must handle only checked exceptions.
The throw keyword in Java is used for explicitly throwing a single exception. This can be from within a method or any block of code. Both checked and unchecked exceptions can be thrown using the throw keyword.
You can't throw two exceptions. I.e. you can't do something like: try { throw new IllegalArgumentException(), new NullPointerException(); } catch (IllegalArgumentException iae) { // ... } catch (NullPointerException npe) { // ... }
With Exceptions, you can either:
try catch
block to handle them. (It's my problem)
throws
in your method specification. (It's not my problem, you deal with it)
For the latter possibility, the caller of the method has the same two options again. As you can see, the Exceptions
can be declared as throws
all the way up your program flow. Even static void main(String[] args)
can be declared with these throws
clauses, which will eventually mean your application will crash when one of the declared Exception
is thrown.
Let's see the following example:
/**
* Throws a MyException.
*/
public static void methodThrowsException() throws MyException {
throw new MyException();
}
/**
* Calls methodThrowsException(), and handles the thrown MyException
*/
public static void myMethod() {
try {
methodThrowsException();
} catch(MyException e) {
/* Do something */
}
}
/**
* Calls methodThrowsException, but does not handle the thrown MyException.
*/
public static void mySecondMethod() throws MyException {
methodThrowsException();
}
When calling the myMethod
or mySecondMethod
, the following happens:
public static void main(String[] args) {
myMethod(); /* No catching needed */
try {
mySecondMethod();
} catch(MyException e){
/* Catching needed */
}
}
As you can see, mySecondMethod
has just transferred the problem of catching the MyException
to its caller.
Suppose you have methodA, which gets called by methodB, which gets called by methodC, and so on...
Example 1:
methodA throws IOException.
Then methodB has to either throw it as well, or catch it which puts an end to the throwing. (Let's say it throws it).
So methodC has to either throw it or catch it (again, let's say it throws it).
Lets say methodD is actually the main method. Again, you have to throw it or catch it. If you throw it here, we are at the start point, there is nowhere to throw it to, so this will stop the program exeuting. Or you can catch it.
Example 2:
methodA throws IOException.
methodB again has to catch it or throw it. This time, it catches it.
methodC no longer has to throw or catch it, because it is dealt with in methodB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With