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 – InputStream to Reader
@Order in Spring
Java equals() and hashCode() Contracts
Hướng dẫn sử dụng Printing Service trong Java
Check if there is mail waiting
Java Program to Generate All Subsets of a Given Set in the Gray Code Order
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Java Program to Implement Bloom Filter
Java Program to implement Priority Queue
Java Program to Generate Random Hexadecimal Byte
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Spring Boot - Interceptor
Receive email using POP3
Introduction to Spring Boot CLI
Java Program to Perform Matrix Multiplication
Spring WebFlux Filters
A Guide to the Java LinkedList
What is a POJO Class?
Dockerizing a Spring Boot Application
Java Program to Describe the Representation of Graph using Incidence List
A Quick Guide to Spring Cloud Consul
Truyền giá trị và tham chiếu trong java
How to Get All Dates Between Two Dates?
Feign – Tạo ứng dụng Java RESTful Client
Hướng dẫn Java Design Pattern – Memento
Introduction to Using FreeMarker in Spring MVC
Thao tác với tập tin và thư mục trong Java
Java Program to Describe the Representation of Graph using Adjacency Matrix
Wiring in Spring: @Autowired, @Resource and @Inject
Hướng dẫn Java Design Pattern – Template Method
Java Program to Implement Sorted Singly Linked List