| 1 |
|
| 2 |
|
| 3 |
This library contains several modules, connected into hierarchical |
| 4 |
structure, Very frequently an error that has happened in lower-level |
| 5 |
module results in breaking the functioning of some ( or all ) parent |
| 6 |
modules. These error states can be conveniently handled by technique |
| 7 |
called 'exception handling'. It's relatively rarely used part of C++ |
| 8 |
standard, and some time ago it was unsupported by popular compilers. |
| 9 |
Here I'll describe briefly how to use exception handling to correctly |
| 10 |
deal with errors in programs on top of Avifile, as well as basics of |
| 11 |
it for those without proper education in this area. |
| 12 |
|
| 13 |
1. EXCEPTION HANDLING BASICS |
| 14 |
|
| 15 |
One basic idea is an 'exception object' - an object that's created |
| 16 |
when exceptional state occurs. Then it's 'thrown' - program repeatedly |
| 17 |
unwinds stack, moving from function to function until either exception |
| 18 |
is catched or program reaches top of stack. In latter case by default |
| 19 |
program aborts ( though one can override default action ). Throwing, |
| 20 |
catching exceptions and creation of exception objects is done by |
| 21 |
programmer, so he can decide what kind of data will be contained |
| 22 |
in exception object. It is convenient for this object to contain |
| 23 |
description of error and module which caused it. |
| 24 |
|
| 25 |
Look at first sample. It shows how to throw an exception and how to catch it. |
| 26 |
|
| 27 |
FILE* f; |
| 28 |
try |
| 29 |
{ |
| 30 |
f=fopen("./my.file", "rb"); |
| 31 |
if(f==NULL)throw "Error opening file"; |
| 32 |
// |
| 33 |
// this code won't be executed if f is NULL |
| 34 |
//.... |
| 35 |
// |
| 36 |
} |
| 37 |
catch(const char* string) |
| 38 |
{ |
| 39 |
//Perform failure handling |
| 40 |
cerr<<string<<endl; |
| 41 |
} |
| 42 |
|
| 43 |
At line 4 we construct an exception object of type const char* and throw it. |
| 44 |
All exceptions of this type that are thrown from inside try-block will be |
| 45 |
immediately caught by catch(const char* string), and inside its block 'string' |
| 46 |
will have the value specified in throw statement. |
| 47 |
|
| 48 |
Slightly more advanced sample: |
| 49 |
|
| 50 |
FILE* f; |
| 51 |
void OpenFile() throw(const char*) // 'throw(...)' is optional |
| 52 |
{ |
| 53 |
f=fopen("./my.file", "rb"); |
| 54 |
if(f==NULL)throw "Error opening file"; |
| 55 |
// |
| 56 |
//... |
| 57 |
// |
| 58 |
} |
| 59 |
void MyFunc() |
| 60 |
{ |
| 61 |
try |
| 62 |
{ |
| 63 |
OpenFile(); |
| 64 |
// |
| 65 |
//... |
| 66 |
// |
| 67 |
} |
| 68 |
catch(const char* string) |
| 69 |
{ |
| 70 |
cerr<<string<<endl; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
It does the same as first one. |
| 75 |
|
| 76 |
There can be more than one 'catch' for each 'try', so you can handle |
| 77 |
different exception objects differently. 'catch(...)' catches ALL |
| 78 |
exceptions, without regard to their types. |
| 79 |
You can add 'throw;' inside catch-block. It'll re-throw caught exception. |
| 80 |
|
| 81 |
2. USING EXCEPTIONS |
| 82 |
|
| 83 |
All files that will work with exceptions must include header <except.h>, |
| 84 |
which contains definition of main exception object types. |
| 85 |
|
| 86 |
--- Catching exceptions --- |
| 87 |
|
| 88 |
Embrace all Avifile calls which can fail into try{} block. Add catch() |
| 89 |
for type FatalError&: |
| 90 |
|
| 91 |
try |
| 92 |
{ |
| 93 |
pFile->OpenFile(argv[1]); |
| 94 |
stream=pFile->GetStream(AviStream::Video, 0); |
| 95 |
} |
| 96 |
catch(FatalError& error) |
| 97 |
{ |
| 98 |
//Here you can show message box with content from error.GetDesc() |
| 99 |
//and re-throw exception if you can't handle absence of stream object |
| 100 |
} |
| 101 |
|
| 102 |
FatalError class has 4 members: Print(), PrintAll(), GetDesc() and GetModule(). |
| 103 |
Print() prints name of module and error description to stderr. PrintAll() does the |
| 104 |
same, but also adds name of C++ source file and line where an exception was |
| 105 |
thrown. GetDesc() returns const char* pointer to error description, and |
| 106 |
GetModule() returns name of failed module. |
| 107 |
|
| 108 |
--- Throwing exceptions --- |
| 109 |
|
| 110 |
In addition to including <except.h>, #define string constant __MODULE__ in your |
| 111 |
source file to contain name of your module: |
| 112 |
|
| 113 |
#include <except.h> |
| 114 |
#define __MODULE__ "MyProg" |
| 115 |
|
| 116 |
Make sure that this definition is not overridden by any included later files. |
| 117 |
Every time you face with an error which is fatal for your module, add line |
| 118 |
|
| 119 |
throw FATAL("...."); |
| 120 |
|
| 121 |
where "...." should contain error definition. |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
4. EXCEPTIONS VS ERROR CODES |
| 127 |
|
| 128 |
|
| 129 |
try |
| 130 |
{ |
| 131 |
CallSomeFunc(); |
| 132 |
CallSomeMoreFunc(); |
| 133 |
DoSomething(); |
| 134 |
} |
| 135 |
catch(FatalError& error) |
| 136 |
{ |
| 137 |
error.Print(); |
| 138 |
destroy(); |
| 139 |
} |
| 140 |
|
| 141 |
/********** |
| 142 |
or: |
| 143 |
**********/ |
| 144 |
|
| 145 |
int hr; |
| 146 |
hr=CallSomeFunc(); |
| 147 |
if(hr!=0) |
| 148 |
{ |
| 149 |
cout<<"CallSomeFunc(): "<<ErrorDescription(hr)<<endl; |
| 150 |
destroy(); |
| 151 |
} |
| 152 |
else |
| 153 |
{ |
| 154 |
hr=CallMoreFunc(); |
| 155 |
if(hr!=0) |
| 156 |
{ |
| 157 |
cout<<"CallMoreFunc(): "<<ErrorDescription(hr)<<endl; |
| 158 |
destroy(); |
| 159 |
} |
| 160 |
else |
| 161 |
{ |
| 162 |
hr=DoSomething(); |
| 163 |
if(hr!=0) |
| 164 |
{ |
| 165 |
cout<<"DoSomething(): "<<ErrorDescription(hr)<<endl; |
| 166 |
destroy(); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|