Categories: Keyword in Java

The try keyword in Java programming language

The try keyword in Java programming language

We will discuss in this tutorial about The try keyword in java programming language

In Java language, the try keyword is used to create try block for exception handling to enclose the code that might throw an exception.

try block caught the exception and handled there.  try block must inside the main method

Java try block may contain both catch and finally block.

Syntax

the syntax of Java try-catch block

try{
    //code that may be throw exception
}catch(Exception_class_Name){
    //this block will only handle if any Arithmetic exception
}

Example

Pr0gram 2

public class try_keyword1{
    public static void main(String args[]){
        try{//try block for exception handling
            int i=0;
            int j=43/i;
            System.out.println(j);

            }catch(ArithmeticException e){
                System.out.println("divide by zero error\n");
            }
    
}
}

 

When the above code is compiled and executed, it will produce the following result

divide by zero error

the syntax of Java try-finally block

try{

//code may be throw exception

}finally(//may be some code){}

Example

Pr0gram 2

public class try_keyword{
    public static void main(String args[]){
        try{//try block for exception handling
            int a[]=new int[5];
            a[6]=45;

            }
            finally{System.out.println("I always executed\n");
    }
    }
}

 

When the above code is compiled and executed, it will produce the following result

Output
 

syntex of Java try-catch-finally block

try{

//code may be throw exception
}catch(Exception_class_Name){}

}finally(//may be some code){}

Example

Program

public class try_keyword{
    public static void main(String args[]){
        try{//try block for exception handling
            int a[]=new int[5];
            a[6]=45;

            }catch(ArrayIndexOutOfBoundsException e){
                System.out.print("Array index out of bound exception\n");
                }
            finally{System.out.println("Exception found\n");}
            System.out.println("Exit from try-catch block\n");
    }
    }

 

When the above code is compiled and executed, it will produce the following result

Array index out of bound exception
Exception found
Exit from try-catch block

 

Multiple catch block under try block in Java

public class try_keyword1{
    public static void main(String args[]){
        try{//try block for exception handling
            int i=0;
            int j=43/i;
            int a[]=new int[4];
            a[7]=45;
            System.out.println(j);

            }catch(ArithmeticException e){
                System.out.println("divide by zero error\n");
            }catch(ArrayIndexOutOfBoundsException e){
                System.out.println("Array index out of bound exception\n");
                }
            finally{System.out.println("Exception found\n");}
            System.out.println("Exit from try-catch block\n");
    }

 

When the above code is compiled and executed, it will produce the following result

devide by zero error
Exception found
Exit from try-catch block

 

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 catch keyword in Java programming language
The finally 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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago