Table of Contents
Có một vài quy tắc về xử lý ngoại lệ (exception handling) với ghi đè (overriding) phương thức trong java như sau:
1. Trường hợp phương thức của lớp cha không khai báo ném ra exception
Nếu phương thức của lớp cha không khai báo ném ra exception, phương thức được ghi đè của lớp cha không thể khai báo ném ra ngoại lệ checked, nhưng ngoại lệ unchecked thì có thể.
1.1. Ví dụ lớp con không thể khai báo ném ra ngoại lệ checked
class Parent {
void msg() {
System.out.println("parent");
}
}
import java.io.IOException;
public class ExceptionChildExample extends Parent {
void msg() throws IOException { // không thể khai báo ném ra ngoại lệ checked
System.out.println("TestExceptionChild");
}
public static void main(String args[]) {
Parent p = new ExceptionChildExample();
p.msg();
}
}
Thực thi chương trình trên:
Compile Time Error
1.2. Ví dụ lớp con có thể khai báo ném ra ngoại lệ unchecked
class Parent {
void msg() {
System.out.println("parent");
}
}
public class ExceptionChildExample extends Parent {
void msg() throws ArithmeticException { // có thể khai báo ném ra ngoại lệ unchecked
System.out.println("TestExceptionChild");
}
public static void main(String args[]) {
Parent p = new ExceptionChildExample();
p.msg();
}
}
Thực thi chương trình trên:
TestExceptionChild
2. Trường hợp phương thức của lớp cha khai báo ném ra exception
Nếu phương thức của lớp cha khai báo ném ra exception, phương thức được ghi đè của lớp cha có thể khai báo ném ra ngoại lệ tương tự, ngoại lệ con, nhưng không thể khai báo ném ra ngoại lệ cha.
2.1. Ví dụ phương thức ghi đè của lớp cha khai báo ném ra ngoại lệ cha
class Parent {
void msg() throws ArithmeticException {
System.out.println("parent");
}
}
public class ExceptionChildExample extends Parent {
// Exception Exception is not compatible with throws clause in Parent.msg()
void msg() throws Exception { // Không thể khai báo ném ra ngoại lệ cha
System.out.println("child");
}
public static void main(String args[]) {
Parent p = new ExceptionChildExample();
try {
p.msg();
} catch (Exception e) {
}
}
}
Thực thi chương trình trên:
Compile Time Error
2.2. Ví dụ phương thức ghi đè của lớp cha khai báo ném ra ngoại lệ tương tự
class Parent {
void msg() throws Exception {
System.out.println("parent");
}
}
public class ExceptionChildExample extends Parent {
void msg() throws Exception {
System.out.println("child");
}
public static void main(String args[]) {
Parent p = new ExceptionChildExample();
try {
p.msg();
} catch (Exception e) {
}
}
}
Thực thi chương trình trên:
child
2.3. Ví dụ phương thức ghi đè của lớp cha khai báo ném ra ngoại lệ con
class Parent {
void msg() throws Exception {
System.out.println("parent");
}
}
public class ExceptionChildExample extends Parent {
void msg() throws ArithmeticException {
System.out.println("child");
}
public static void main(String args[]) {
Parent p = new ExceptionChildExample();
try {
p.msg();
} catch (Exception e) {
}
}
}
Thực thi chương trình trên:
child
2.4. Ví dụ phương thức ghi đè của lớp cha không khai báo ném ra ngoại lệ nào
class Parent {
void msg() throws Exception {
System.out.println("parent");
}
}
public class ExceptionChildExample extends Parent {
void msg() {
System.out.println("child");
}
public static void main(String args[]) {
Parent p = new ExceptionChildExample();
try {
p.msg();
} catch (Exception e) {
}
}
}
Thực thi chương trình trên:
child
Related posts:
Java Program to Implement Karatsuba Multiplication Algorithm
Java Program to Implement the Checksum Method for Small String Messages and Detect
Java Program to Implement Jarvis Algorithm
Java Program to Implement RenderingHints API
Create a Custom Auto-Configuration with Spring Boot
Mapping a Dynamic JSON Object with Jackson
Composition, Aggregation, and Association in Java
Spring Boot - Cloud Configuration Client
Format ZonedDateTime to String
Removing all duplicates from a List in Java
Control Structures in Java
Spring Boot - Creating Docker Image
String Initialization in Java
Java Program to Construct K-D Tree for 2 Dimensional Data
Java Program to Implement Stack API
Java Program to Implement Adjacency List
Java Program to Implement Graph Structured Stack
Java Program to Implement the MD5 Algorithm
Hướng dẫn Java Design Pattern – DAO
TreeSet và sử dụng Comparable, Comparator trong java
Java Program to Implement Shoelace Algorithm
Spring Cloud – Adding Angular
Java Program to Implement the RSA Algorithm
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Java Program to Implement D-ary-Heap
A Guide to the finalize Method in Java
Java Program to Implement PriorityQueue API
Java Program to Implement Bit Array
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Java Program to Implement Ternary Heap
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)