RAD Studio |  |

Exception handling
Collapse All
Topics

Name | Description
Unwinding Exceptions (C++) | When an exception is thrown, the runtime library takes the thrown object, gets the type of the object, and looks upward in the call stack for a handler whose type matches the type of the thrown object. Once a handler is found, the RTL unwinds the stack to the point of the handler, and executes the handler. In the unwind process, the RTL calls destructors for all local objects in the stack frames between where the exception was thrown and where it is caught. If a destructor causes an exception to be raised during stack unwinding and does not handle... more
Exception Handling Options (C++) | The following exception handling options are available for bcc32:
Constructors In Exception Handling (C++) | Class constructors can throw exceptions if they cannot successfully construct an object. If a constructor throws an exception, that object's destructor is not necessarily called. Destructors are called only for the base classes and for those objects that were fully constructed inside the classes since entering the try block. Note: This does not apply to VCL base classes.
Defining Protected Blocks | To prepare for exceptions, you place statements that might raise them in a try block. If one of these statements does raise an exception, control is transferred to an exception handler that handles that type of exception, then leaves the block. The exception handler is said to catch the exception and specifies the actions to take. By using try blocks and exception handlers, you can move error checking and error handling out of the main flow of your algorithms, resulting in simpler, more readable code. You start a protected block using the keyword try. The exception handler must... more
Exception Handling | Exceptions are exceptional conditions that require special handling. They include errors that occur at runtime, such as divide by zero, and the exhaustion of free store. Exception handling provides a standard way of dealing with errors, discovering both anticipated and unanticipated problems, and enables developers to recognize, track down, and fix bugs. When an error occurs, the program raises an exception, meaning it creates an exception object and rolls back the stack to the first point it finds where you have code to handle the exception. The exception object usually contains information about what happened. This allows another part of... more
Exception-handling Statements | The exception handling block starts with the except keyword and ends with the keyword end. These two keywords are actually part of the same statement as the try block. That is, both the try block and the exception handling block are considered part of a single try...except statement. Inside the exception handling block, you include one or more exception handlers. An exception handler is a statement of the form
Throwing An Exception (C++) | To raise an exception in C++, use the throw keyword. Objects in C++ can be thrown by value, or pointer:
Raising an Exception | To indicate a disruptive error condition, you can raise an exception by constructing an instance of an exception object that describes the error condition and calling the reserved word raise. To raise an exception, call the reserved word raise, followed by an instance of an exception object. This establishes the exception as coming from a particular address. When an exception handler actually handles the exception, it finishes by destroying the exception instance, so you never need to do that yourself. For example, given the following declaration,
Reraising Exceptions | Sometimes when you handle an exception locally, you want to augment the handling in the enclosing block, rather than replace it. Of course, when your local handler finishes its handling, it destroys the exception instance, so the enclosing block's handler never gets to act. You can, however, prevent the handler from destroying the exception, giving the enclosing handler a chance to respond. You do this by using the raise command with no arguments. This is called reraising or rethrowing the exception. The following example illustrates this technique:
Smart Pointers (C++) | If you have local variables that are pointers to objects and an exception is thrown, these pointers are not automatically deleted. This is because there is no good way for the compiler to distinguish between a pointer to data that was allocated for this function only and any other pointer. The class that you can use to ensure that objects allocated for local use are destroyed in the even of an exception is auto_ptr. There is a special case in which memory is freed for a pointer allocated in a function:
Scope of Exception Handlers | You do not need to provide handlers for every kind of exception in every block. You only need handlers for exceptions that you want to handle specially within a particular block. If a block does not handle a particular exception, execution leaves that block and returns to the block that contains it (or returns to the code that called the block), with the exception still raised. This process repeats with increasingly broad scope until either execution reaches the outermost scope of the application or a block at some level handles the exception. Thus, you can nest your exception handling code.... more
Silent Exceptions | VCL applications handle most exceptions that your code doesn't specifically handle by displaying a message box that shows the message string from the exception object. You can also define "silent" exceptions that do not, by default, cause the application to show the error message. Silent exceptions are useful when you don't intend to report an exception to the user, but you want to abort an operation. Aborting an operation is similar to using the Break or Exit procedures to break out of a block, but can break out of several nested levels of blocks. Silent exceptions all descend from the... more
Writing a finally Block (C++) | bcc32 includes extensions to the C++ language that let it use finally blocks as well. Like exception handlers, a finally block must appear directly after the try block, but it is introduced by the__finally keyword instead of catch.
Writing a Finally Block (Delphi) | Finally blocks are introduced by the keyword finally. They are part of a try..finally statement, which has the following form:
Writing Exception Handlers | The exception handling block appears immediately after the try block. This block incudes one or more exception handlers. An exception handler provides a specific response to a specific kind of exception. Handling an exception clears the error condition and destroys the exception object, which allows the application to continue execution. You typically define exception handlers to allow your applications to recover from errors and continue running. Types of exceptions you might handle include attempts to open files that don't exist, writing to full disks, or calculations that exceed legal bounds. Some of these, such as "File not found," are... more
Writing finally Blocks | An exception handler is code that handles a specific exception or exceptions that occur within a protected block of code. However, there are times when you do not need to handle the exception, but you do have code that you want to execute after the protected block, even if an exception occurs. Typically, such code handles cleanup issues, such as freeing resources that were allocated before the protected block. By using finally blocks, you can ensure that if your application allocates resources, it also releases them, even if an exception occurs. Thus, if your application allocates memory, you can make... more
Writing the Try Block | The first part of a protected block is the try block. The try block contains code that can potentially raise an exception. The exception can be raised either directly in the try block, or by code that is called by statements in the try block. That is, if code in a try block calls a routine that doesn't define its own exception handler, then any exceptions raised inside that routine cause execution to pass to the exception-handler associated with the try block. Keep in mind that exceptions don't come just from your code. A call to an RTL routine or... more
Handling Classes of Exceptions | Exceptions are always represented by classes. As such, you usually work with a hierarchy of exception classes. For example, VCL defines the ERangeError exception as a descendant of EIntError. When you provide an exception handler for a base exception class, it catches not only direct instances of that class, but instances of any of its descendants as well. For example, the following exception handler handles all integer math exceptions, including ERangeError, EDivByZero, and EIntOverflow:
VCL Exception Classes | VCL includes a large set of built-in exception classes for automatically handling divide-by-zero errors, file I/O errors, invalid typecasts, and many other exception conditions. All VCL exception classes descend from one root object called Exception. Exception provides a consistent interface for applications to handle exceptions. It provides the string for the message that VCL exceptions display by default. The following table lists a selection of the exception classes defined in VCL: Selected exception classes
Default Exception Handling in VCL | If your application code does not catch and handle the exceptions that are raised, the exceptions are ultimately caught and handled by the HandleException method of the global Application object. For all exceptions but EAbort, HandleException calls the OnException event handler, if one exists. If there is no OnException event handler (and the exception is not EAbort), HandleException displays a message box with the error message associated with the exception. There are certain circumstances where HandleException does not get called. Exceptions that occur before or after the execution of the application's Run method are not caught and handled... more
Defining Your Own VCL Exceptions | Because VCL exceptions are classes, defining a new kind of exception is as simple as declaring a new class type. Although you can raise any object instance as an exception, the standard VCL exception handlers handle only exceptions that descend from Exception. New exception classes should be derived from Exception or one of the other standard exceptions. That way, if you raise your new exception in a block of code that isn't protected by an exception handler specific to that exception, one of the standard handlers will handle it instead. For example, consider the following declaration:
Handling Exceptions in VCL Applications | If you use VCL components or the VCL runtime library in your applications, you need to understand the VCL exception handling mechanism. Exceptions are built into many VCL classes and routines and they are thrown automatically when something unexpected occurs. Typically, these exceptions indicate programming errors that would otherwise generate a runtime error. A limited number of these classes is described in VCL Exception Classes. The mechanics of handling component exceptions are no different than handling any other type of exception. If you do not handle the exception, VCL handles it in a default manner. Typically, a message... more

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!