In this article, we will discuss differences between throws-clause and try-catch-finally block in Java
1. Java Exception handling:
There are 5 keywords related to Exception Handling; those are
- try
- catch
- throw
- throws
- finally
2. try-catch-finally combination:
We can use 3 combination of try-catch-finally in Java methods; those combination are
- try-catch
- try-finally
- try-catch-finally
3. throw and throws (with an extra S):
- throw is used to throw exception from executing block; it could be try-block or catch-block (from inside a method)
- throws is used to indicate that particular method is possibly throws these exceptions; again method can throws any number of exceptions
4. Difference between throws clause and try-catch block ?
- As explained above, throws clause is used to indicate that particular exception is possibly thrown from executing method at run-time
- Whereas try-catch block is used to handle exception scenario
- Like, if any particular exception is thrown from try block;
- then corresponding catch-block catches that particular exception and we can take necessary action from there ON (this is some kind of coding stuff)
try-catch block | throws keyword |
Using try-catch block, we can handle exception surrounding code that might raise an exception | Whereas using throws keyword, we can simply declare exception that might raise from that method |
Caught exception in the catch block can be re-thrown after some alteration | There is no such flexibility, as its directly throws exception |
try-catch block ensures graceful termination for that particular method
Except one scenario when catch block throws exception | Doesn’t guarantee graceful termination
In most cases, throws declaration leads to abnormal termination |
Hope, it help to understand most of the things related to Java Exception Handling
Related Articles:
- Java – Exception Handling
- Java – Exception Hierarchy
- Java – 5 important keywords in Java Exception handling
- Java – Runtime mechanism, what happens when exception is thrown ?
- Java – Checked Exception v/s Unchecked Exception
- Java – Exception propagation
- Java – try-catch block
- Java – finally block
- Java – try with multiple catch blocks
- Java – Nested try-catch block
- Java – Returning value from method having try-catch-finally blocks
- Java – return statement with finally block
- Java – final v/s finally v/s finalize
- Java – Various methods to print exception information
- Java – throw keyword
- Java – throws keyword
- Java – throw v/s throws
- Java – Difference between throws clause and try-catch-finally block
- Java – Rules for Exception handling w.r.t Method Overriding
- Java – User-defined or Custom exception
- Java – Difference between ClassNotFoundException v/s NoClassDefFoundError
- Java – Top Exception and Error
- Java – Interview question and answers on Exception Handling
- Java 7 – try with resources
- Java 7 – multi-catch block
References:
- https://www.benchresources.net/java/exception-handling/
- https://www.benchresources.net/try-catch-block-in-java-exception-handling/
- https://www.benchresources.net/finally-block-in-java-exception-handling/
- https://www.benchresources.net/throw-keyword-in-java-exception-handling/
- https://www.benchresources.net/difference-between-throw-vs-throws-keyword-in-java-exception-handling/
- https://www.quora.com/What-is-the-main-difference-between-throws-and-try-catch-in-Java
Happy Coding !!
Happy Learning !!