Java Exception Keywords

Java provides try, catch, finally, throw, throws keywords to dealing with exceptions. try-catch, finally are used for handle exceptions. ‘throw‘ is used to throw our own exception. ‘throws‘ is used to declare an exception.

tryWe have to place risky code inside “try” block and the corresponding handling code inside catch block. (The code which may cause an exception is called risky code)
catchThe “catch” block is used to handle the exception which is raised in try block. It should be preceded by try block which means we can’t use catch block alone without try block. It can be followed by finally block later.
finallyfinally block contains all the crucial statements that must be executed whether exception occurs or not. Crucial statements could be closing a connection, stream, file etc. (‘cleanup’ or ‘resource releasing’ code).
throwThe “throw” keyword is used to throw an exception by developer when business rules of an application is violated.
throwsThe “throws” keyword is used to declare exceptions as part of method signature. It doesn’t really throw an exception. It just specifies that, method body code may raise an execution in its execution.
Java Exception keywords
Scroll to Top