មេរៀនទី៥២: ExceptionHandling ជាមួយMethodOverriding

ឧទាហរណ៍ក្នុងករណីនេះ subclass overridden method ប្រកាស declares ដូច exception
import java.io.*;
class Parent{
void msg()throws Exception{System.out.println(“parent”);}
}
class TestExceptionChild3 extends Parent{
void msg()throws Exception{System.out.println(“child”);}
public static void main(String args[]){
Parent p=new TestExceptionChild3();
try{
p.msg();
}catch(Exception e){}
}
}
Output:child
ឧទាហរណ៍ករណីនេះ subclass overridden method ប្រកាស declares subclass exception
import java.io.*;
class Parent{
void msg()throws Exception{System.out.println(“parent”);}
}
class TestExceptionChild4 extends Parent{
void msg()throws ArithmeticException{System.out.println(“child”);}
public static void main(String args[]){
Parent p=new TestExceptionChild4();
try{
p.msg();
}catch(Exception e){}
}
}
Output:child
ឧទាហរណ៍ករណី subclass overridden method ប្រកាស declares no exception
import java.io.*;
class Parent{
void msg()throws Exception{System.out.println(“parent”);}
}
class TestExceptionChild5 extends Parent{
void msg(){System.out.println(“child”);}
public static void main(String args[]){
Parent p=new TestExceptionChild5();
try{
p.msg();
}catch(Exception e){}
}
}
Output:child