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:
LIKE Queries in Spring JPA Repositories
Java Program to Implement Min Heap
Reactive WebSockets with Spring 5
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Handling Errors in Spring WebFlux
Jackson vs Gson
Java Program to Implement ConcurrentHashMap API
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
Java Program to Implement Stack API
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Most commonly used String methods in Java
Spring Security Remember Me
Hướng dẫn Java Design Pattern – DAO
Java Program to Implement Unrolled Linked List
Java Program to Perform Left Rotation on a Binary Search Tree
Java String Conversions
A Guide to JUnit 5 Extensions
Spring Boot - Scheduling
Spring Security OAuth Login with WebFlux
Explain about URL and HTTPS protocol
Java Program to Find the Connected Components of an UnDirected Graph
Java Program to Implement Sorted Vector
Java Program to Solve Tower of Hanoi Problem using Stacks
Constructor Injection in Spring with Lombok
Java Program to Compare Binary and Sequential Search
Serverless Functions with Spring Cloud Function
Java – Reader to InputStream
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Introduction to Spring Data JDBC
Java Program to Implement Tarjan Algorithm
Java Program to implement Bit Matrix
Converting String to Stream of chars