Defining Exceptions

An exception is a special kind of error that can be captured and handled by the program. It is a general term for a series of abnormal behaviors that occur during program execution, for example, out-of-bounds array access, division by zero, arithmetic overflow, invalid input, and so on. To ensure correct operation and robustness, many software systems contain a large amount of code for error detection and error handling.

Exceptions break the normal execution flow of a program. Once an exception occurs, the program must handle it immediately, that is, transfer control from its normal function to the part that handles the exception. Cangjie provides an exception handling mechanism to handle various exceptions that may occur during program running.

In Cangjie, exception types are class types, and there are two base exception classes: Error and Exception.

  • Error and its subclasses describe internal system errors and resource exhaustion errors. If an internal error occurs, you are notified to terminate the program safely.
  • Exception and its subclasses describe exceptions caused by logic errors or I/O errors during program running, such as out-of-bounds array access or an attempt to open a file that does not exist. Such exceptions need to be captured and processed by the program.

You cannot customize exceptions by inheriting the Error class or any of its subclasses; they are reserved for the representation of built-in Cangjie errors. To create your own exception class, you can inherit the built-in Exception class or one of its subclasses. For example:

open class FatherException <: Exception {
    public init() {
        super("This is FatherException.")
    }
    public init(message: String) {
        super(message)
    }
    public open override func getClassName(): String {
        "FatherException"
    }
}

class ChildException <: FatherException {
    public init() {
        super("This is ChildException.")
    }
    public open override func getClassName(): String {
        "ChildException"
    }
}

The following table lists the main functions of Exception and their descriptions.

Function TypeFunction and Description
Constructorinit() is the default constructor.
Constructorinit(message: String) can be used to specify a custom exception message.
Member attributeopen prop message: String returns the details about the exception. This message is initialized in the constructor of the exception class and is an empty string by default.
Member functionopen func toString(): String returns the type name and details of the exception. The exception details call messages by default.
Member functionfunc getClassName(): String returns the user-defined class name. Rewrite this method for subclasses to return the subclass name.
Member functionfunc printStackTrace(): Unit prints stack trace information to the standard error stream.

The following table lists the main functions of Error and their descriptions.

Function TypeFunction and Description
Member attributeopen prop message: String returns the details about the error. This message is initialized internally when an error occurs and is an empty string by default.
Member functionopen func toString(): String returns the type name and details of the error. The error details call messages by default.
Member functionfunc printStackTrace(): Unit prints stack trace information to the standard error stream.