មេរៀនទី៤៨: multiple catch block
ប្រសិនបើលោកអ្នក មានទម្រង់ពីរខុសគ្នា នៃ Exceptions, អាចប្រើ multple catch block.
ឧទាហរណ៍នៃ multiple catch block
public class TestMultipleCatchBlock{
public static void main(String args[]){
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e){System.out.println(“task1 is completed”);}
catch(ArrayIndexOutOfBoundsException e){System.out.println(“task 2 completed”);}
catch(Exception e){System.out.println(“common task completed”);}
System.out.println(“rest of the code…”);
}
}
Output:task1 completed
rest of the code…
នៅពេលណាមួយ Exception ត្រូវបានកើតឡើង ហើយម៉ោង គ្រាន់តែជា catch block មួយត្រូវបានប្រត្តិបត្តិ។ គ្រប់
class TestMultipleCatchBlock1{
public static void main(String args[]){
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(Exception e){System.out.println(“common task completed”);}
catch(ArithmeticException e){System.out.println(“task1 is completed”);}
catch(ArrayIndexOutOfBoundsException e){System.out.println(“task 2 completed”);}
System.out.println(“rest of the code…”);
}
}
Output:Compile-time error
ឧទាហរណ៍នៃ multiple catch block
public class TestMultipleCatchBlock{
public static void main(String args[]){
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e){System.out.println(“task1 is completed”);}
catch(ArrayIndexOutOfBoundsException e){System.out.println(“task 2 completed”);}
catch(Exception e){System.out.println(“common task completed”);}
System.out.println(“rest of the code…”);
}
}
Output:task1 completed
rest of the code…
នៅពេលណាមួយ Exception ត្រូវបានកើតឡើង ហើយម៉ោង គ្រាន់តែជា catch block មួយត្រូវបានប្រត្តិបត្តិ។ គ្រប់
class TestMultipleCatchBlock1{
public static void main(String args[]){
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(Exception e){System.out.println(“common task completed”);}
catch(ArithmeticException e){System.out.println(“task1 is completed”);}
catch(ArrayIndexOutOfBoundsException e){System.out.println(“task 2 completed”);}
System.out.println(“rest of the code…”);
}
}
Output:Compile-time error
Post a Comment