← Back to lessons

51 Exceptions

Hard

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.

In Cangjie we make use of try catch blocks.

Custom exceptions can be defined as subclasses of the exception type. Note you can not create custom exceptions of the error type.

Here we create custom exception Father which inherits from the Exception type.

Finally is excecuted after the try catch statement. The content of a finally block is executed regardless of whether an exception occurs

exceptions.cj
open class Father <: Exception {
    var father: Int32 = 0
}

class ChildOne <: Father {
    var childOne: Int32 = 1
}

class ChildTwo <: Father {
    var childTwo: Int32 = 2
}

main(): Int64 {
    try {
        throw IllegalArgumentException("This is an Exception!")
    } catch (e: IllegalArgumentException) {
        println(e.message)
        println("IllegalArgumentException is caught!")
    } catch (e: ChildTwo | ChildOne) {
        println("ChildTwo or ChildOne?")
    } finally {
        println("finally is executed!")
    }

    return 0
}

// Output:
// This is an Exception!
// IllegalArgumentException is caught!
// finally is executed!