We will discuss in this tutorial about The catch keyword in Java programming language
The catch is a keyword of Java, it is a part of a try block using exception handling in Java. When an exception is thrown inside a try block, the exception will be compared to any of the catch parts of the inside in try block. When the exception match with one of the exceptions in any one catch part, the exception will be handled there.
The syntax of catch block (inside of try) in java
try { //statements which may throw exception } catch (ApplicationException e) { //when try block throws exception //catch block will be executed }
Java allowed multiple catch block inside the try block
We can write multiple catch block in the single try block. When the exception match with one of the exceptions in any one catch part. The exception will be handled there
The syntax of multiple catches (inside of try block) in java
try { statement_A; statement_B; ................ } catch(Exception Type1) { //handle the exception_Type_1 here statements.. } catch(Exception Type2) {//handle the exception_Type_2 here statements.. }
if the try block throws Exception_Type_1 then it will be handled in its catch block(first catch block). Similarly, wen the try block throws Exception_Type_2 then it will be handled in the second catch block.
You can handle multiple types of exceptions in a single catch block. this is known as multi-catch block multi-catch is introduced from Java 7 version
try{ //Code that may throw an exception } catch(IO Exception | IndexOutOfBoundException e){ //handled both exceptions here(multiple exceptions) } catch(Exception e){ //handled other exceptions here }
There are other Java language keywords that are similar to this keyword
static keyword in Java language
extends keyword in Java language
Suggested for you
Usage of final keyword in Java
Finally keyword in Java language
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…