Apabila mengatasi kaedah superclass, anda perlu mengikut peraturan tertentu jika kaedah itu memberikan pengecualian.
Jika kaedah superclass melontar pengecualian tertentu, kaedah dalam subkelas harus membuang pengecualian yang sama atau subjenisnya.
Dalam contoh berikut, kaedah readFile() superclass membuang IOException, manakala kaedah readFile() subclass membuang FileNotFoundException.
Memandangkan pengecualian FileNotFoundException ialah subjenis IOException, atur cara boleh menyusun dan melaksanakan tanpa sebarang ralat.
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super { public String readFile(String path) throws IOException { throw new IOException(); } } public class ExceptionsExample extends Super { @Override public String readFile(String path) throws FileNotFoundException { Scanner sc = new Scanner(new File("E://test//sample.txt")); String input; StringBuffer sb = new StringBuffer(); while (sc.hasNextLine()) { input = sc.nextLine(); sb.append(" "+input); } return sb.toString(); } public static void main(String args[]) { String path = "E://test//sample.txt"; ExceptionsExample obj = new ExceptionsExample(); try { System.out.println(obj.readFile(path)); }catch(FileNotFoundException e) { System.out.println("Make sure the specified file exists"); } } }
Tutorials Point is an E-learning company that set out on its journey to provide knowledge to that class of readers that responds better to online content. With Tutorials Point, you can learn at your own pace, in your own space. After a successful journey of providing the best learning content at tutorialspoint.com, we created our subscription based premium product called Tutorix to provide Simply Easy Learning in the best personalized way for K-12 students, and aspirants of competitive exams like IIT/JEE and NEET.
Begitu juga, jika subkelas membuang pengecualian yang sama seperti superclass, atur cara akan menyusun dan melaksanakan dengan jayanya.
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super { public void sampleMethod()throws FileNotFoundException { System.out.println("Method of superclass"); } } public class ExceptionsExample extends Super { public void sampleMethod()throws FileNotFoundException { System.out.println("Method of Subclass"); } public static void main(String args[]) { ExceptionsExample obj = new ExceptionsExample(); obj.sampleMethod(); } }
Method of Subclass
Jika kaedah superclass melontar pengecualian, kaedah dalam subkelas tidak seharusnya membuang superclassnya.
Dalam contoh berikut, kaedah readFile() superclass membuang pengecualian FileNotFoundException, manakala kaedah readFile() subkelas membuang pengecualian IOException, iaitu superclass FileNotFoundException.
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super { public String readFile(String path)throws FileNotFoundException { throw new FileNotFoundException(); } } public class ExceptionsExample extends Super { @Override public String readFile(String path)throws IOException { //method body ...... } }
Apabila disusun, atur cara di atas memberikan output berikut -
ExceptionsExample.java:13: error: readFile(String) in ExceptionsExample cannot override readFile(String) in Sup public String readFile(String path)throws IOException { ^ overridden method does not throw IOException 1 error
Jika kaedah kelas induk membuang beberapa pengecualian, anda boleh mengatasinya tanpa membuang sebarang pengecualian .
Dalam contoh di bawah, kaedah sampleMethod() kelas induk membuang pengecualian FileNotFoundException, manakala kaedah sampleMethod() subkelas tidak membuang sebarang pengecualian sama sekali. Walau bagaimanapun, program menyusun dan melaksanakan tanpa sebarang ralat.
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super { public void sampleMethod()throws FileNotFoundException { System.out.println("Method of superclass"); } } public class ExceptionsExample extends Super { public void sampleMethod() { System.out.println("Method of Subclass"); } public static void main(String args[]) { ExceptionsExample obj = new ExceptionsExample(); obj.sampleMethod(); } }
Method of Subclass
Atas ialah kandungan terperinci Apakah peraturan untuk liputan kaedah dan pengendalian pengecualian di Jawa?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!