The try keyword in Java programming language
- Home
- Keyword in Java
- The try keyword in Java programming language
- On
- By
- 0 Comment
- 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
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
static keyword in Java language
extends keyword in Java language
Suggested for you
Usage of final keyword in Java
Finally keyword in Java language