Example & Tutorial understanding programming in easy ways.

what are the the main keyword used in exceptation handling?

There are mainly four keyword used in java exception handling. They are as follows:-

  1. throw: Sometimes we explicitly want to create exception object and then throw it to interupt the normal processing of the program. throw keyword is used to throw exception to the runtime to handle it. It can also be used with the main method and the the caller of the method handels the exceptations

  2. throws: When we are throwing any checked exception in a method and not handling it, then we need to use throws keyword in method signature to let caller program know the exceptions that might be thrown by the method. The caller method might handle these exceptions or propagate it to it’s caller method using throws keyword. We can provide multiple exceptions in the throws clause and it can be used with main() method also.

  3. try-catch: We use try-catch block for exception handling in our code. try is the start of the block and catch is at the end of try block to handle the exceptions. We can have multiple catch blocks with a try and try-catch block can be nested also. catch block requires a parameter that should be of type Exception.

  4. finally: finally block is optional and can be used only with try-catch block. Since exception halts the process of execution, we might have some resources open that will not get closed, so we can use finally block. finally block gets executed always, whether exception occurrs or not.



Read More →