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:
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Collection trong java
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Introduction to Netflix Archaius with Spring Cloud
Java Program to Find the Vertex Connectivity of a Graph
New Features in Java 14
Java Program to Implement Max-Flow Min-Cut Theorem
Spring Boot - Logging
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Giới thiệu Google Guice – Binding
Java Program to Perform the Sorting Using Counting Sort
JWT – Token-based Authentication trong Jersey 2.x
Java Program to Implement LinkedList API
Java Program to Implement Bellman-Ford Algorithm
Java Program to Implement Leftist Heap
Java Program to Compute Determinant of a Matrix
Guide to Java 8 groupingBy Collector
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Spring Data Reactive Repositories with MongoDB
Serve Static Resources with Spring
ClassNotFoundException vs NoClassDefFoundError
The Thread.join() Method in Java
Java Program to Implement Binomial Tree
How to Set TLS Version in Apache HttpClient
Getting Started with GraphQL and Spring Boot
Split a String in Java
Java Program to Perform Quick Sort on Large Number of Elements
Hướng dẫn Java Design Pattern – DAO
Java Program to Implement Lloyd’s Algorithm
Java Program to Implement the Hill Cypher
Receive email using POP3