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:
Introduction to Spring Data REST
The HttpMediaTypeNotAcceptableException in Spring MVC
Java Program to implement Associate Array
Spring Cloud – Tracing Services with Zipkin
Sort a HashMap in Java
Spring Boot - Actuator
Java Program to Represent Graph Using Incidence Matrix
Multipart Upload with HttpClient 4
Java Program to Print only Odd Numbered Levels of a Tree
Spring Boot - Eureka Server
Spring MVC + Thymeleaf 3.0: New Features
Guide to Character Encoding
Checked and Unchecked Exceptions in Java
Java Program to Implement Max-Flow Min-Cut Theorem
A Guide to TreeMap in Java
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
An Introduction to ThreadLocal in Java
Guide to @ConfigurationProperties in Spring Boot
Introduction to Eclipse Collections
Custom Exception trong Java
Java Program to Perform Complex Number Multiplication
Chuyển đổi Array sang ArrayList và ngược lại
Prevent Cross-Site Scripting (XSS) in a Spring Application
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Java Program to Implement Floyd-Warshall Algorithm
Java Program to Implement Counting Sort
LinkedHashSet trong java
Check If a String Is Numeric in Java
Java Program to Generate Random Hexadecimal Byte
Java Program to Implement Booth Algorithm
Guide To CompletableFuture
New Features in Java 10