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 Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Java Program to Implement HashMap API
Number Formatting in Java
How to Get the Last Element of a Stream in Java?
Reading an HTTP Response Body as a String in Java
Lớp Arrarys trong Java (Arrays Utility Class)
Java Program to Implement Hash Tables with Linear Probing
Apache Commons Collections OrderedMap
Versioning a REST API
Lập trình đa luồng trong Java (Java Multi-threading)
Guide to Dynamic Tests in Junit 5
Introduction to Eclipse Collections
How to Get All Spring-Managed Beans?
Java Program to Find Path Between Two Nodes in a Graph
Prevent Brute Force Authentication Attempts with Spring Security
Java Program to Implement Meldable Heap
Java Program to find the maximum subarray sum using Binary Search approach
Tìm hiểu về Web Service
How to Convert List to Map in Java
Spring RestTemplate Request/Response Logging
So sánh HashSet, LinkedHashSet và TreeSet trong Java
Java Program to Implement LinkedHashMap API
Java Program to Implement the RSA Algorithm
Bootstrapping Hibernate 5 with Spring
Optional trong Java 8
Converting Iterator to List
Java Timer
Guide to the ConcurrentSkipListMap
Removing all duplicates from a List in Java
Java Program to Implement Merge Sort Algorithm on Linked List
A Quick Guide to Spring MVC Matrix Variables
Introduction to Project Reactor Bus