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

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