fatherazrael
fatherazrael

Reputation: 5987

When should we choose to throw an exception?

When should we chose to throws an exception?

 public Something sqlQuery(String sqlQuer) throws SqlException {

 }

We can catch this exception in try catch.

In which situation does we choose to use throws instead of catching immediately? is it some design pattern related?

Upvotes: -1

Views: 73

Answers (1)

Lecagy
Lecagy

Reputation: 489

If a method (M1) has the capability to handle an exception by itself, then use try-catch-finally. If not, throw it. The calling method (M2) now has to handle a potential exception of M1 or throw it itself.

Beside of this, all Expections expect of RuntimeExceptions require to be catched.

Upvotes: 0

Related Questions