⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exceptions.cpp

📁 《C Builder 5技术内幕》程序源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Exceptions.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cspin"
#pragma resource "*.dfm"
TMainFormExceptions *MainFormExceptions;
//extern TReferenceStream *pReferenceStream;

//---------------------------------------------------------------------------
__fastcall TMainFormExceptions::TMainFormExceptions(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainFormExceptions::ButtonDividedByZeroClick(TObject *Sender)
{
  int i = 630, j = 0, k;
  try
  {
    k = i / j;
    ShowMessage( k );
  }
  catch( EDivByZero& e )
  {
    MessageDlg( "Error: Divided by zero.", mtError, TMsgDlgButtons() << mbOK, 0 );
  }
}
//---------------------------------------------------------------------------
void __fastcall TMainFormExceptions::ButtonConvertClick(TObject *Sender)
{
  int i;
  try
  {
    i = StrToInt( "Dear June" );
    ShowMessage( i );
  }
  catch( EConvertError& e )
  {
    AnsiString S1("Class where error occurred: " + this->ClassName());
    AnsiString S2("Type of error: " + e.ClassName());
    MessageDlg(S1 + '\r' + S2 + '\r' + e.Message, mtError,
      TMsgDlgButtons() << mbOK, 0);
  }
}
//---------------------------------------------------------------------------
void __fastcall TMainFormExceptions::FormCreate(TObject *Sender)
{
  LoadAllToolButtonImages();
}
//---------------------------------------------------------------------------
void __fastcall TMainFormExceptions::LoadAllToolButtonImages()
{
  LoadToolButtonImage( ImageListButtons, "res\\pgmd_n.bmp" );
  LoadToolButtonImage( ImageListHotButtons, "res\\pgmd_h.bmp" );
  LoadToolButtonImage( ImageListDisableButtons, "res\\pgmd_d.bmp" );

  LoadToolButtonImage( ImageListButtons, "res\\pgsr_n.bmp" );
  LoadToolButtonImage( ImageListHotButtons, "res\\pgsr_h.bmp" );
  LoadToolButtonImage( ImageListDisableButtons, "res\\pgsr_d.bmp" );

  LoadToolButtonImage( ImageListButtons, "res\\append_n.bmp" );
  LoadToolButtonImage( ImageListHotButtons, "res\\append_h.bmp" );
  LoadToolButtonImage( ImageListDisableButtons, "res\\append_d.bmp" );

  LoadToolButtonImage( ImageListButtons, "res\\insert_n.bmp" );
  LoadToolButtonImage( ImageListHotButtons, "res\\insert_h.bmp" );
  LoadToolButtonImage( ImageListDisableButtons, "res\\insert_d.bmp" );

  LoadToolButtonImage( ImageListButtons, "res\\change_n.bmp" );
  LoadToolButtonImage( ImageListHotButtons, "res\\change_h.bmp" );
  LoadToolButtonImage( ImageListDisableButtons, "res\\change_d.bmp" );

  LoadToolButtonImage( ImageListButtons, "res\\delete_n.bmp" );
  LoadToolButtonImage( ImageListHotButtons, "res\\delete_h.bmp" );
  LoadToolButtonImage( ImageListDisableButtons, "res\\delete_d.bmp" );

  LoadToolButtonImage( ImageListButtons, "res\\pack_n.bmp" );
  LoadToolButtonImage( ImageListHotButtons, "res\\pack_h.bmp" );
  LoadToolButtonImage( ImageListDisableButtons, "res\\pack_d.bmp" );

  LoadToolButtonImage( ImageListButtons, "res\\criter_n.bmp" );
  LoadToolButtonImage( ImageListHotButtons, "res\\criter_h.bmp" );
  LoadToolButtonImage( ImageListDisableButtons, "res\\criter_d.bmp" );

  LoadToolButtonImage( ImageListButtons, "res\\find_n.bmp" );
  LoadToolButtonImage( ImageListHotButtons, "res\\find_h.bmp" );
  LoadToolButtonImage( ImageListDisableButtons, "res\\find_d.bmp" );

  LoadToolButtonImage( ImageListButtons, "res\\save_n.bmp" );
  LoadToolButtonImage( ImageListHotButtons, "res\\save_h.bmp" );
  LoadToolButtonImage( ImageListDisableButtons, "res\\save_d.bmp" );

  LoadToolButtonImage( ImageListButtons, "res\\output_n.bmp" );
  LoadToolButtonImage( ImageListHotButtons, "res\\output_h.bmp" );
  LoadToolButtonImage( ImageListDisableButtons, "res\\output_d.bmp" );

  LoadToolButtonImage( ImageListButtons, "res\\first_n.bmp" );
  LoadToolButtonImage( ImageListHotButtons, "res\\first_h.bmp" );
  LoadToolButtonImage( ImageListDisableButtons, "res\\first_d.bmp" );

  LoadToolButtonImage( ImageListButtons, "res\\last_n.bmp" );
  LoadToolButtonImage( ImageListHotButtons, "res\\last_h.bmp" );
  LoadToolButtonImage( ImageListDisableButtons, "res\\last_d.bmp" );

  LoadToolButtonImage( ImageListButtons, "res\\prev_n.bmp" );
  LoadToolButtonImage( ImageListHotButtons, "res\\prev_h.bmp" );
  LoadToolButtonImage( ImageListDisableButtons, "res\\prev_d.bmp" );

  LoadToolButtonImage( ImageListButtons, "res\\next_n.bmp" );
  LoadToolButtonImage( ImageListHotButtons, "res\\next_h.bmp" );
  LoadToolButtonImage( ImageListDisableButtons, "res\\next_d.bmp" );
//  Yor can do this like that:
//    pBitmapButton = new Graphics::TBitmap;
//    pBitmapButton->LoadFromFile( "res\\last_n.bmp" );
//    ImageListButtons->Add( pBitmapButton, NULL );
//    if( pBitmapButton != NULL )
//    {
//      delete pBitmapButton;
//      pBitmapButton = NULL;
//    }
//    pBitmapButton = new Graphics::TBitmap;
//    pBitmapButton->LoadFromFile( "res\\last_h.bmp" );
//// NEVER WORK! I DON'T FIND ANY HELP ON HInstance!
////    int nThisInstance = FindHInstance( Application );
////    int nResInstance = FindResourceHInstance( nThisInstance );
////    pBitmapButton->LoadFromResourceName( nResInstance, "BITMAP_LAST_H" );
//    ImageListHotButtons->Add( pBitmapButton, NULL );
//// NEVER WORK TOO! I DON'T KNOW WHY!
////    ImageListHotButtons->ResourceLoad( rtBitmap, "BITMAP_LAST_H", NULL );
//    if( pBitmapButton != NULL )
//    {
//      delete pBitmapButton;
//      pBitmapButton = NULL;
//    }
//    pBitmapButton = new Graphics::TBitmap;
//    pBitmapButton->LoadFromFile( "res\\last_h.bmp" );
//    ImageListDisableButtons->Add( pBitmapButton, NULL );
//    if( pBitmapButton != NULL )
//    {
//      delete pBitmapButton;
//      pBitmapButton = NULL;
//    }
}
//---------------------------------------------------------------------------
bool __fastcall TMainFormExceptions::LoadToolButtonImage( TImageList *pDest, AnsiString asSrc )
{
//  C++Builder 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 encapsulates the fundamental properties and methods for
//  all exceptions and provides a consistent interface for applications
//  to handle exceptions.
//  You can pass exceptions to a catch block that takes a parameter of
//  type Exception. Use the following syntax to catch VCL exceptions:
//
//  catch (const exception_class &exception_variable)
//
//  You specify the exception class that you want to catch and
//  provide a variable by which to refer to the exception.
//  Following is an example of how to throw a VCL exception:
//
//  void __fastcall TForm1::ThrowException(TObject *Sender)
//  {
//    try
//    {
//      throw Exception("VCL components?");
//    }
//    catch(const Exception &E)
//    {
//      ShowMessage(AnsiString(E.ClassName())+ E.Message);
//   }
//  }
//
//  The throw statement in the previous example creates an instance of
//  the Exception class and calls its constructor. All exceptions
//  descended from Exception have a message that can be displayed,
//  passed through constructors, and retrieved through the Message property.
//
//  Selected VCL exception classes are described below.
//
//  EAbort          	Stops a sequence of events without displaying an error message dialog box.
//  EAccessViolation	Checks for invalid memory access errors.
//  EBitsError      	Prevents invalid attempts to access a Boolean array.
//  EComponentError	    Signals an invalid attempt to register or rename a component.
//  EConvertError    	Indicates string or object conversion errors.
//  EDatabaseError	    Specifies a database access error.
//  EDBEditError    	Catches data incompatible with a specified mask.
//  EDivByZero      	Catches integer divide-by-zero errors.
//  EExternalException	Signifies an unrecognized exception code.
//  EInOutError     	Represents a file I/O error.
//  EIntOverflow	    Specifies integer calculations whose results are too large for the allocated register.
//  EInvalidCast    	Checks for illegal typecasting.
//  EInvalidGraphic	    Indicates an attempt to work with an unrecognized graphic file format.
//  EInvalidOperation   Occurs when invalid operations are attempted on a component.
//  EInvalidPointer	    Results from invalid pointer operations.
//  EMenuError      	Involves a problem with menu item.
//  EOleCtrlError	    Detects problems with linking to ActiveX controls.
//  EOleError       	Specifies OLE automation errors.
//  EPrinterError	    Signals a printing error.
//  EPropertyError  	Occurs on unsuccessful attempts to set the value of a property.
//  ERangeError	        Indicates an integer value that is too large for the declared type to which it is assigned.
//  ERegistryException	Specifies registry errors.
//  EStackOverflow	    Occurs when the stack grows into the final guard page.
//  EZeroDivide     	Catches floating-point divide-by-zero errors.
//
//
//  EInOutError is thrown when an operating-system file input/output error
//  occurs, provided I/O checking is enabled.
//  To enable or disable I/O checking, choose Project|Options and
//  select the Pascal tab.
//  The resulting error code is returned in the local ErrorCode variable,
//  which can take the following values.
//  2	File not found.
//  3	Invalid file name.
//  4	Too many open files.
//  5	Access denied.
//  100	EOF.
//  101	Disk full.
//  106	Invalid input.
//  If an I/O error occurs when I/O checking is disabled,
//  the application must call the IOResult function to clear the error.
//
//  Message contains the text string to display in the exception dialog box
//  when the exception is raised.
//
//  __property System::AnsiString Message = {read=FMessage, write=FMessage};
//
//  Description:
//  Message stores the error-message string to display when the exception
//  is raised. All Exception constructors expect a string parameter to
//  store in Message. Message text can be hard-coded as a parameter to
//  an Exception constructor, created as dynamic parameter, or loaded from
//  a resource file as a static or dynamically formatted parameter.
//
  Graphics::TBitmap *pBitmapButton;
// Try to allocate memory for bitmap graphics.
// If fails, that is, insufficient memory can result in that
// no memory is allocated. An excepts will occur.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -