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 Optimize Wire Length in Electrical Circuit
Spring Boot: Customize Whitelabel Error Page
Java Program to Find kth Largest Element in a Sequence
Tìm hiểu về xác thực và phân quyền trong ứng dụng
Java Program to Implement Dijkstra’s Algorithm using Queue
Java 8 Predicate Chain
Use Liquibase to Safely Evolve Your Database Schema
Luồng Daemon (Daemon Thread) trong Java
Introduction to Spring Cloud OpenFeign
Creating a Web Application with Spring 5
Spring Boot - CORS Support
An Intro to Spring Cloud Task
Introduction to Spring MVC HandlerInterceptor
Java Program to Implement Trie
Hướng dẫn Java Design Pattern – Factory Method
The Java 8 Stream API Tutorial
Java – InputStream to Reader
Java Program to Show the Duality Transformation of Line and Point
Implementing a Runnable vs Extending a Thread
Introduction to Java Serialization
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Spring Boot - Unit Test Cases
Java – String to Reader
Introduction to Eclipse Collections
ExecutorService – Waiting for Threads to Finish
Java Program to Implement Interpolation Search Algorithm
Java Program to Find Basis and Dimension of a Matrix
Spring Boot - Batch Service
Java Program to Check Multiplicability of Two Matrices
Java Program to Implement Pollard Rho Algorithm
Getting the Size of an Iterable in Java
Set Interface trong Java