Razvan
Razvan

Reputation: 5

Is there a need to catch exceptions that I've speciffied in throws clause?

Is there a need to catch exceptions that I've specified in throws clause? And viceversa, is there a need to specify exceptions catched?

public method() throws IOException, SQLException {
     ...
     try {
          ....
     }catch(IOException | SQLException ex) {
          ex.getMessage();
     }
}

Upvotes: 0

Views: 107

Answers (7)

0o'-Varun-'o0
0o'-Varun-'o0

Reputation: 735

There are two types of Exceptions:

Checked and Unchecked.

This page says:

Checked: are the exceptions that are checked at compile time

Unchecked are the exceptions that are not checked at compiled time

            +-----------+
          | Throwable |
            +-----------+
             /         \
          /           \
      +-------+          +-----------+
      | Error |          | Exception |
      +-------+          +-----------+
    /  |  \            /     |    \
      \______/            \______/     \
                                   +------------------+
    unchecked       checked        | RuntimeException |
                                   +------------------+
                                    /   |    |      \
                                    \________________/

                                        unchecked

so this a general rule if a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception

Also on this site, it says:

Exception Handling is mainly used to handle the checked exceptions. If there occurs any unchecked exception such as NullPointerException, it is programmers fault that he is not performing check up before the code being used.

Upvotes: -1

Shir
Shir

Reputation: 263

The concept of catch exception is to indicate that some thing go wrong.To the user and to the developer. always is good to catch the exception and write to log what is wrong. The more detailes is better, it's easier to fix code. But if you specific exception like sqlException you can skip another kind of exceptions that can be thrown.

Upvotes: 0

Steve Chaloner
Steve Chaloner

Reputation: 8202

Your method doesn't need to declare it throws those exceptions, because you catch them within the method. throws should be used when an exception can be thrown out of a method and must be handled further up the call tree.

In this example, MyCheckedException is an exception that may be thrown out of your method and must be handled by the calling method, either by having that method catch the exception or also declare that it throws it.

public method() throws MyCheckedException {
    ...
    try {
        ....
    } catch(IOException | SQLException ex) {
        ex.getMessage();
    }
}

If an exception is or extends RuntimeException, you don't need to declare that it's thrown (but there are deeper design issues here).

Upvotes: 0

Ralph Marshall
Ralph Marshall

Reputation: 223

The short answer is "no". If you catch the exceptions in your code then you won't throw them (as previous comment noted), and thus there is no need to declare them in the throws clause. Conversely, if you're trying to throw them to your callers you shouldn't catch them. The only exception to that is that you might want to catch the exception and log it here for some reason, and then re-throw it for handling farther up the stack.

Upvotes: 0

Prasad Kharkar
Prasad Kharkar

Reputation: 13566

  • You can declare a method that throws Exception if you want the calling method to catch the exception.
  • If you want that same method should handle exception, then you have to catch it.
  • You should either catch exceptions of declare the method as throws

Upvotes: 0

npinti
npinti

Reputation: 52195

You could be in a situation where you need to catch any exceptions for logging purposes or for actual business logic: If this operation fails, then, use this operation as backup.

In your case, you are swallowing exceptions, since it would seem that nothing is being done with what ex.getMessage() is returning, which is not recommended.

Upvotes: 0

HJK
HJK

Reputation: 1382

There is no explicit need to catch the exceptions that were specified in the signature.

Ideally the answer depends, If you want to propagate the exception to the caller then, don't catch the exception. Let the caller know about the exceptoin and decide on what to do on next, like showing a message, recovery actions, cleanup etc. Its a good practise not to catch the exception

Upvotes: 2

Related Questions