មេរៀនទី៤៦: Exception Handling
exception handling in java ប្រើក្នុង mechanism to handle the runtime errors ដែលជា flow ធម្មតានៃកម្មវិធី application ។
What is exception handling
Exception Handling ជា mechanism ទទួលខុសត្រូវខាង runtime errors ដូចជា ClassNotFound, IO, SQL, Remote ។
គុណប្រយោជន៍នៃ Exception Handling
exception handling គឺជា maintain the normal flow of the application
statement 1;
statement 2;
statement 3;
statement 4;
statement 5;//exception occurs
statement 6;
statement 7;
statement 8;
statement 9;
statement 10;
ឧបមានថា 10 statements ក្នុងកម្មវិធីរបស់អ្នក និងកើតឡើង exception នៅ statement 5, rest កូដមិនប្រត្តិបត្តិការ statement 6 ដល់10 នឹងមិនរត់។
Hierarchy of Exception classes
ប្រភេទនៃ Exception
មានពីរប្រភេទចម្បងនៃ exceptions: checked និង unchecked កន្លែង error ដូចជា unchecked exception។ sun microsystem មានបីប្រភេទនៃ exceptions:
1. Checked Exception
2. Unchecked Exception
3. Error
ភាពខុសគ្នារវាង checked និង unchecked exceptions
1) Checked Exception
classes ដែលពង្រីក Throwable class except RuntimeException និង Error គឺដូចជា checked exceptions ឧទាហរណ៍.IOException, SQLException ។ Checked exceptions បាន checked ក្នុង compile-time។
2) Unchecked Exception
classes ដែលពង្រីក RuntimeException គឺដឹងដូចជា unchecked exceptions ឧទាហរណ៍ ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException ។ Unchecked exceptions គឺមិន checked នៅពេល compile-time ទោះបីបាន នៅពេល runtime។
3) Error
Error គឺ OutOfMemoryError, VirtualMachineError, AssertionError etc.
Common scenarios where exceptions may occur
1) Scenario where ArithmeticException occurs
If we divide any number by zero, there occurs an ArithmeticException.
int a=50/0;//ArithmeticException
2) Scenario where NullPointerException occurs
If we have null value in any variable, performing any operation by the variable occurs an NullPointerException.
String s=null;
System.out.println(s.length());//NullPointerException
3) Scenario where NumberFormatException occurs
The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string variable that have characters, converting this variable into digit will occur NumberFormatException.
String s=”abc”;
int i=Integer.parseInt(s);//NumberFormatException
4) Scenario where ArrayIndexOutOfBoundsException occurs
If you are inserting any value in the wrong index, it would result ArrayIndexOutOfBoundsException as shown below:
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException
What is exception handling
Exception Handling ជា mechanism ទទួលខុសត្រូវខាង runtime errors ដូចជា ClassNotFound, IO, SQL, Remote ។
គុណប្រយោជន៍នៃ Exception Handling
exception handling គឺជា maintain the normal flow of the application
statement 1;
statement 2;
statement 3;
statement 4;
statement 5;//exception occurs
statement 6;
statement 7;
statement 8;
statement 9;
statement 10;
ឧបមានថា 10 statements ក្នុងកម្មវិធីរបស់អ្នក និងកើតឡើង exception នៅ statement 5, rest កូដមិនប្រត្តិបត្តិការ statement 6 ដល់10 នឹងមិនរត់។
Hierarchy of Exception classes

មានពីរប្រភេទចម្បងនៃ exceptions: checked និង unchecked កន្លែង error ដូចជា unchecked exception។ sun microsystem មានបីប្រភេទនៃ exceptions:
1. Checked Exception
2. Unchecked Exception
3. Error
ភាពខុសគ្នារវាង checked និង unchecked exceptions
1) Checked Exception
classes ដែលពង្រីក Throwable class except RuntimeException និង Error គឺដូចជា checked exceptions ឧទាហរណ៍.IOException, SQLException ។ Checked exceptions បាន checked ក្នុង compile-time។
2) Unchecked Exception
classes ដែលពង្រីក RuntimeException គឺដឹងដូចជា unchecked exceptions ឧទាហរណ៍ ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException ។ Unchecked exceptions គឺមិន checked នៅពេល compile-time ទោះបីបាន នៅពេល runtime។
3) Error
Error គឺ OutOfMemoryError, VirtualMachineError, AssertionError etc.
Common scenarios where exceptions may occur
1) Scenario where ArithmeticException occurs
If we divide any number by zero, there occurs an ArithmeticException.
int a=50/0;//ArithmeticException
2) Scenario where NullPointerException occurs
If we have null value in any variable, performing any operation by the variable occurs an NullPointerException.
String s=null;
System.out.println(s.length());//NullPointerException
3) Scenario where NumberFormatException occurs
The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string variable that have characters, converting this variable into digit will occur NumberFormatException.
String s=”abc”;
int i=Integer.parseInt(s);//NumberFormatException
4) Scenario where ArrayIndexOutOfBoundsException occurs
If you are inserting any value in the wrong index, it would result ArrayIndexOutOfBoundsException as shown below:
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException
Post a Comment