Keyword in Java

The catch keyword in Java programming language

The catch keyword in java programming language

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.

Declaration

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 allows multiple catch block

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.

 

Multiple exceptions in a single 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

try keyword in Java

Catch keyword in Java

implements keyword in java

public Key words in Java

static keyword in Java language

Volatile Keyword in Java

Transient keyword in Java

Package keyword in Java

class keyword in Java

boolean keyword in Java

break keyword in Java

do keyword in Java

double keyword in Java

This keyword in Java

final keyword in Java

switch keyword in Java

interface keyword in java

extends keyword in Java language

 

Suggested for you

 Java language Keywords

C language Keywords

 Python language Keywords

keywords in C language

Usage of final keyword in Java

Usage of this keyword in Java

Finally keyword in Java language

 

 

 

 

 

 

The continue keyword in Java programming language
The try keyword in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

6 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

6 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

6 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

9 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

1 year ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

1 year ago