The function of the exception class is to perform some of our own function functions in the event of an unexpected situation, in order to easily find the source of the error, can throw an exception when an unexpected occurrence
(1). First define your own exception class can be directly defined or derived from the standard exception class
Class Ceguiexport Exception
{
Public
Virtual ~exception (void);
Const string& getMessage (void) const {return d_message;}
Const string& GetName () const {return d_name;}
Const string& GetFileName (void) const {return d_filename;}
const int GetLine (void) const {return d_line;}
Protected
Exception (const string& message = "", const string& name = "Cegui::exception", const string& filename = "", int line = 0);
String D_message;
String D_filename;
String D_name;
int d_line;
};
//------------------------------------------------------------------------
Exception::exception (const string& message, const string& name, const string& filename, int line)
: D_filename (filename), d_line (line), D_message (message), D_name (name)
{
Log exception or send it to error stream (if logger not available)
logger* Logger = Logger::getsingletonptr ();
if (logger)
{
Logger->logevent (name + "in file" + filename + "(" + propertyhelper::inttostring (line) + "):" + message, Errors);
}
Else
{
Std::cerr << name << "in file" << filename.c_str () << "(" << Line << "):" << Message.c_str () << Std::endl;
}
}
//------------------------------------------------------------------------
Exception::~exception (void)
{
}
(2). Derive a specific exception class here is the exception that is inconvenient to categorize
Class Ceguiexport Unknownobjectexception:public Exception
{
Public
Unknownobjectexception (const string& message, const string& file = "Unknown", int line = 0)
: Exception (Message, "Cegui::unknownobjectexception", file, line) {}
};
#define UNKNOWNOBJECTEXCEPTION (message) \
Unknownobjectexception (Message, __file__, __line__)
(3). In a specific function function, set the throw exception where an exception may occur
Const image& imageset::getimage (const string& name) const
{
Imageregistry::const_iterator pos = d_images.find (name);
if (pos = = D_images.end ())//here may cause an exception because the graphics file could not be found in order to handle such an accident can be thrown at this point
{
Throw Unknownobjectexception ("The Image named '" + name + "' could not being found in ImageSet '" + d_name + "'."); When an exception is thrown, an exception class object is created, and the constructor of the exception class is executed to perform some special behaviors that we assign to the exception class, such as outputting error messages to the log.
}
Return pos->second;
}
(4). Use the try{} catch () {} block to intercept and process the exception when using a function function that may be unexpected
Try
{
Image = &imagesetmanager::getsingleton (). Getimageset (ImageSet)->getimage (imageName);
}
catch (unknownobjectexception&)
{
Image = 0;
}
Below is a Win32 console under the Exceptiondemo
1 //ExceptionDemo.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <afx.h>6#include <string>7#include <afxwin.h>8 9 using namespacestd;Ten One A classException - { - Public: the Virtual~exception (void) {} - Const string& GetMessage (void)Const{returnd_message;} - Const string& GetName ()Const{returnD_name;} - Const string& GetFileName (void)Const{returnD_filename;} + Const intGetLine (void)Const{returnD_line;} - protected: +Exception (Const string& message ="",Const string& name ="exceptiondemo::exception", A Const string& filename ="",intline =0): d_filename (filename), d_line (line), D_message (message), D_name (name) at { - CString str; -Str. Format ("Name:%s in File:%s (%d):%s", Name.c_str (), Filename.c_str (), Line,message.c_str ()); - AfxMessageBox (str); - } - stringD_message; in stringD_filename; - stringD_name; to intD_line; + }; - the classUnknownobjectexception: PublicException * { $ Public:Panax NotoginsengUnknownobjectexception (Const string& Message,Const string& file ="Unknown",intline =0) -: Exception (Message,"exceptiondemo::unknownobjectexception", file, line) the { + A } the }; + - #defineUnknownobjectexception (Message) $ unknownobjectexception (Message, __file__, __line__) $ - - voidfunctionintN) the { - if(n>5)Wuyi ThrowUnknownobjectexception ("Pass the n volue limited!"); the } - Wu int_tmain (intARGC, _tchar*argv[]) - { About Try $ { -function6); - } - Catch(unknownobjectexception&e) A { +AfxMessageBox ("caught the anomaly! "); the } - return 0; $}
Definition and use of C + + exception classes and my understanding of exception classes