RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
Exception Class

Exception is the base class for all runtime exceptions.

Pascal
Exception = class(TObject);
C++
class Exception : public TObject;

Exception encapsulates the fundamental properties and methods for all exceptions. Use Exception as the base class for creating custom exceptions. 

All methods introduced for the Exception object are constructors that provide alternative ways to create exception messages. Some constructors also create help context IDs. Typically, an application calls these constructors dynamically when an exception occurs. 

Exception messages can be hard-coded strings, formatted strings, or strings (including formatted strings ) loaded from an application resource. 

Exceptions are raised when a runtime error occurs in an application, such as attempting to divide by zero. When an exception is raised, typically an exception instance displays a dialog box describing the error condition. If an application does not handle the exception condition, then the default exception handler is called. This handler also displays a dialog box with an OK button that usually permits an application to continue processing when the user clicks OK.

Note: Standard practice is to derive all exception classes from Exception, and specify a name beginning with "E", followed by an abbreviated, descriptive name.
 

C++ Examples: 

 

/*
In addition to displaying the exception message, which
happens by default, the following code shuts down the 
application when an exception is not caught and handled.
AppException should be declared a method of TForm1.
*/

#include <System.hpp>
#include <SysUtils.hpp>

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  Application->OnException = AppException;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::AppException(TObject *Sender, Exception *E)
{
  Application->ShowException(E);
  Application->Terminate();
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  throw(Exception("Hardware error: Divide by 0"));
}

 

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