Java: Java Programming For Beginners a simple Start To Java Programming (Written By a software Engineer)


Download 0.82 Mb.
Pdf ko'rish
bet66/95
Sana09.01.2022
Hajmi0.82 Mb.
#261112
1   ...   62   63   64   65   66   67   68   69   ...   95
Bog'liq
Java Programming For Beginners - A Simple Start to Java Programming ( PDFDrive )

Getting Exceptions:
A  system  discovers  a  special  case  utilizing  a  blend  of  the  try  and  catch  keywords.  A  try
scope  is  set  around  the  code  that  may  produce  an  exemption.  Code  inside  this  scope  is
alluded to as secured code, and the structure for utilizing try/catch is given below:
try {
//Code that may produce an exception


}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

Download 0.82 Mb.

Do'stlaringiz bilan baham:
1   ...   62   63   64   65   66   67   68   69   ...   95




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling