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.
try{ //code that may be throw exception }catch(Exception_class_Name){ //this block will only handle if any Arithmetic exception }
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
try{ //code may be throw exception }finally(//may be some code){}
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
try{ //code may be throw exception }catch(Exception_class_Name){} }finally(//may be some code){}
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
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
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…