Which exception to throw c




















This is the handler that catches the exception that's thrown if the types in the throw and catch expressions are compatible. For a list of rules that govern type-matching in catch blocks, see How Catch Blocks are Evaluated. If the catch statement specifies an ellipsis Because catch blocks are processed in program order to find a matching type, an ellipsis handler must be the last handler for the associated try block. Use catch Typically, a catch A throw expression that has no operand re-throws the exception currently being handled.

Such an expression should only be used in a catch handler or in a function that's called from a catch handler. The re-thrown exception object is the original exception object, not a copy. This is done using a throw keyword. The catch keyword indicates the catching of an exception. It's followed by one or more catch blocks.

Assuming a block will raise an exception, a method catches an exception using a combination of the try and catch keywords. You can list down multiple catch statements to catch different type of exceptions in case your try block raises more than one exception in different situations. If used in a single-task environment, the list root can simply be implemented as a single global variable. Example 2 shows an improved version using exception handler records on the stack.

XLinkExceptionRecord puts the given exception-handler record onto the current task's exception-handler list and initializes it. XUnLinkExceptionRecord removes the exception-handler record from the list. XRaise throws an exception, which is a negative error code in RTFiles. XRaise retrieves the top-level exception-handler record of the current task and then calls longjmp with the given error code. This transfers control to the correct handler. To simplify the syntax, the exception-handler library's header file defines a few macros to encapsulate building and destroying an exception-handler block.

Example 3 shows the same program using these macros. One big advantage of a home-grown exception handling library is that we can define the semantics to best fit the needs of our application. For example, the most common case in RTFiles is that a function cannot handle an error, but some cleanup action is required. Typically, some global data structures of the filesystem are protected on entry to the filesystem using semaphores. We must ensure that such semaphores are released no matter how the function is left through a normal return statement or through an exception.

If the handler does not explicitly call function XHandled , the exception-handling library will continue to pass the exception to handlers higher up in the list called "exception propagation" or "stack unwinding". We decided on these semantics because they reflect the typical case in RTFiles.

Positive values indicate success and negative values indicate errors. To keep things simple, we use the standard RTFiles error codes as exception values. This lets us automate returning correct error codes to the application. Basically, we propagate exceptions out of RTFiles by simply returning the exception value.

This is implemented by the macro XEND, which returns the error code if it finds that the last exception-handler record has been removed and that a still unhandled exception is being propagated. To implement these semantics, the exception-handling library must know the current state of processing, stored in the current top-level exception-handling record. Three states are distinguished:. If the code section completes without raising any exceptions, XUnLinkExceptionRecord is called next, which will then set the state to XFinally and execute the finally-handler.

If, however, an exception is raised, XRaise sets the state to XHandling and calls the appropriate handler. The catch statement takes a parameter : in our example we use an int variable myNum because we are throwing an exception of int type in the try block age , to output the value of age. If no error occurs e.

If you do not know the throw type used in the try block, you can use the "three dots" syntax We just launched W3Schools videos. Get certified by completing a course today! If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:.



0コメント

  • 1000 / 1000