}catch(nameofexception exp_1) {
//Code to be executed once an exception occurs
}
A try block includes announcing the kind of exception you are attempting to get. In the
event that an exception
happens in ensured code, the catch square that executes after the
attempt is checked. In the event that this sort of special case
that happened in a try block,
the exception goes to the catch block, which is also passed as a system parameter.
Sample Implementation:
import java.io.*;
public class MyException{
public static void main(string args[]){
try{
int myarr[] = new int[2];
System.out.println(“This statement attempts to access the third element of the array:” +
a[3]);
}catch(arrayindexoutofboundsexception e_1){
System.out.println(“The thrown exception is: ” + e_1);
}
System.out.println(“Exception: Out of Bounds”);
}
This would deliver the accompanying result:
The thrown exception is: java.lang.arrayindexoutofboundsexception: 3
Exception:
Out of Bounds
Do'stlaringiz bilan baham: