Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch exceptions listed in a throws clause

Tags:

java

exception

How can I catch an exception declared in a throws clause if I don't write a try/catchclause?

  • 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?

  • . When i try to call this function, then its mandatory to write try/catch block or write a throws clause in the calling function. Again, how will i catch these exceptions if i dont write a try/catch block?
like image 949
Diffy Avatar asked Jul 26 '14 11:07

Diffy


People also ask

How do you catch throw exception?

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.

Which type of exception must be caught with a throws clause?

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.

Can we handle exception with throws?

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.

Can you throw multiple exceptions in one throw statement?

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) { // ... }


2 Answers

With Exceptions, you can either:

  • Use try catch block to handle them. (It's my problem)
  • Declare them using 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.

like image 90
nhaarman Avatar answered Oct 04 '22 11:10

nhaarman


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.

like image 31
Chris Avatar answered Oct 04 '22 10:10

Chris