1<?xml version="1.0" encoding="utf-8" ?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 4<head> 5<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 6<title>Magick++ API: Exceptions </title> 7<link rel="stylesheet" href="magick.css" type="text/css" /> 8</head> 9<body> 10<div class="doc-section"> 11<h1 align="center">Magick::Exception Classes</h1> 12<p><i>Exception</i> represents the base class of objects thrown when 13Magick++reports an error. Magick++ throws C++ exceptions synchronous 14with the operation where the error occurred. This allows errors to be 15trapped within the enclosing code (perhaps the code to process a 16single image) while allowing the code to be written with a simple 17coding style.</p> 18<p>A try/catch block should be placed around any sequence of 19operations which can be considered an important body of work. For 20example, if your program processes lists of images and some of these 21images may be defective, by placing the try/catch block around the 22entire sequence of code that processes one image (including 23instantiating the image object), you can minimize the overhead of 24error checking while ensuring that all objects created to deal with 25that object are safely destroyed (C++ exceptions unroll the stack 26until the enclosing try block, destroying any created objects). 27</p> 28<p>The pseudo code for the main loop of your program may look like: 29</p> 30<div class="viewport"> 31using namespace std; 32for infile in list 33{ 34 try { 35 // Construct an image instance first so that we don't have to worry 36 // about object construction failure due to a minor warning exception 37 // being thrown. 38 Magick::Image image; 39 try { 40 // Try reading image file 41 image.read(infile); 42 } 43 catch( Magick::WarningCoder &warning ) 44 { 45 // Process coder warning while loading file (e.g. TIFF warning) 46 // Maybe the user will be interested in these warnings (or not). 47 // If a warning is produced while loading an image, the image 48 // can normally still be used (but not if the warning was about 49 // something important!) 50 cerr << "Coder Warning: " << warning.what() << endl; 51 } 52 catch( Magick::Warning &warning ) 53 { 54 // Handle any other Magick++ warning. 55 cerr << "Warning: " << warning.what() << endl; 56 } 57 catch( Magick::ErrorFileOpen &error ) 58 { 59 // Process Magick++ file open error 60 cerr << "Error: " << error.what() << endl; 61 continue; // Try next image. 62 } 63 try { 64 image.rotate(90); 65 image.write("outfile"); 66 } 67 catch ( MagickExeption & error) 68 { 69 // Handle problem while rotating or writing outfile. 70 cerr << "Caught Magick++ exception: " << error.what() << endl; 71 } 72 } 73 catch( std::exception & error ) 74 { 75 // Process any other exceptions derived from standard C++ exception 76 cerr << "Caught C++ STD exception: " << error.what() << endl; 77 } 78 catch( ... ) 79 { 80 // Process *any* exception (last-ditch effort). There is not a lot 81 // you can do here other to retry the operation that failed, or exit 82 } 83} 84</div> 85<p>The desired location and number of try/catch blocks in your program 86depends how sophisticated its error handling must be. Very simple 87programs may use just one try/catch block.</p> 88<p>The <i>Exception</i> class is derived from the C++ standard 89exception class. This means that it contains a C++ string containing 90additional information about the error (e.g to display to the user). 91Obtain access to this string via the what() method.  For 92example: 93</p> 94<pre class="code"> 95catch( Exception & error_ ) 96 { 97 cout << "Caught exception: " << error_.what() << endl; 98 } 99</pre> 100<p>The classes <i>Warning</i> and <i>Error</i> derive from the 101<i>Exception</i> class. Exceptions derived from <i>Warning</i> are 102thrown to represent non-fatal errors which may effect the 103completeness or quality of the result (e.g. one image provided as an 104argument to montage is defective). In most cases, a <i>Warning</i> 105exception may be ignored by catching it immediately, processing it 106(e.g. printing a diagnostic) and continuing on. Exceptions derived 107from <i>Error</i> are thrown to represent fatal errors that can not 108produce a valid result (e.g. attempting to read a file which does not 109exist). 110</p> 111<p style="margin-bottom: 0in">The specific derived exception classes 112are shown in the following tables: 113</p> 114<p align="center" style="margin-bottom: 0in"><b>Warning Sub-Classes</b></p> 115<ul><table width="100%" border="1" cellpadding="2" cellspacing="3"> 116 <col width="70*" /> 117 <col width="186*" /> 118 <tr> 119 <td width="27%"> 120 <p align="center"><font size="2"><b>Warning</b></font></p> 121 </td> 122 <td width="73%"> 123 <p align="center"><font size="2"><b>Warning Description</b></font></p> 124 </td> 125 </tr> 126 <tr> 127 <td width="27%"> 128 <p align="center"><font size="2">WarningUndefined</font></p> 129 </td> 130 <td width="73%"> 131 <p><font size="2">Unspecified warning type.</font></p> 132 </td> 133 </tr> 134 <tr> 135 <td width="27%"> 136 <p align="center"><font size="2">WarningBlob</font></p> 137 </td> 138 <td width="73%"> 139 <p style="font-weight: medium; text-decoration: none"><font size="2">NOT 140 CURRENTLY USED</font></p> 141 </td> 142 </tr> 143 <tr> 144 <td width="27%"> 145 <p align="center"><font size="2">WarningCache</font></p> 146 </td> 147 <td width="73%"> 148 <p style="font-weight: medium; text-decoration: none"><font size="2">NOT 149 CURRENTLY USED</font></p> 150 </td> 151 </tr> 152 <tr> 153 <td width="27%"> 154 <p align="center"><font size="2">WarningCoder</font></p> 155 </td> 156 <td width="73%"> 157 <p><font size="2">Warnings issued by some coders.</font></p> 158 </td> 159 </tr> 160 <tr> 161 <td width="27%"> 162 <p align="center"><font size="2">WarningConfigure</font></p> 163 </td> 164 <td width="73%"> 165 <p style="font-weight: medium; text-decoration: none"><font size="2">NOT 166 CURRENTLY USED</font></p> 167 </td> 168 </tr> 169 <tr> 170 <td width="27%"> 171 <p align="center"><font size="2">WarningCorruptImage</font></p> 172 </td> 173 <td width="73%"> 174 <p><font size="2">Warning issued when an image is determined to be 175 corrupt.</font></p> 176 </td> 177 </tr> 178 <tr> 179 <td width="27%"> 180 <p align="center"><font size="2">WarningDelegate</font></p> 181 </td> 182 <td width="73%"> 183 <p><font size="2">Warnings reported by the delegate (interface to 184 external programs) subsystem.</font></p> 185 </td> 186 </tr> 187 <tr> 188 <td width="27%"> 189 <p align="center"><font size="2">WarningDraw</font></p> 190 </td> 191 <td width="73%"> 192 <p><font size="2">Warnings reported by the rendering subsystem.</font></p> 193 </td> 194 </tr> 195 <tr> 196 <td width="27%"> 197 <p align="center"><font size="2">WarningFileOpen</font></p> 198 </td> 199 <td width="73%"> 200 <p><font size="2">Warning reported when The image file could not be 201 opened (permission problem, wrong file type, or does not exist).</font></p> 202 </td> 203 </tr> 204 <tr> 205 <td width="27%"> 206 <p align="center"><font size="2">WarningImage</font></p> 207 </td> 208 <td width="73%"> 209 <p style="font-weight: medium"><font size="2">NOT CURRENTLY USED</font></p> 210 </td> 211 </tr> 212 <tr> 213 <td width="27%"> 214 <p align="center"><font size="2">WarningMissingDelegate</font></p> 215 </td> 216 <td width="73%"> 217 <p style="font-weight: medium"><font size="2">NOT CURRENTLY USED</font></p> 218 </td> 219 </tr> 220 <tr> 221 <td width="27%"> 222 <p align="center"><font size="2">WarningModule</font></p> 223 </td> 224 <td width="73%"> 225 <p style="font-weight: medium"><font size="2">NOT CURRENTLY USED</font></p> 226 </td> 227 </tr> 228 <tr> 229 <td width="27%"> 230 <p align="center"><font size="2">WarningMonitor</font></p> 231 </td> 232 <td width="73%"> 233 <p style="font-weight: medium"><font size="2">NOT CURRENTLY USED</font></p> 234 </td> 235 </tr> 236 <tr> 237 <td width="27%"> 238 <p align="center"><font size="2">WarningOption</font></p> 239 </td> 240 <td width="73%"> 241 <p><font size="2">Warning reported when an option is malformed or 242 out of range.</font></p> 243 </td> 244 </tr> 245 <tr> 246 <td width="27%"> 247 <p align="center"><font size="2">WarningRegistry</font></p> 248 </td> 249 <td width="73%"> 250 <p style="font-weight: medium"><font size="2">NOT CURRENTLY USED</font></p> 251 </td> 252 </tr> 253 <tr> 254 <td width="27%"> 255 <p align="center"><font size="2">WarningResourceLimit</font></p> 256 </td> 257 <td width="73%"> 258 <p><font size="2">Warning reported when a program resource is 259 exhausted (e.g. not enough memory).</font></p> 260 </td> 261 </tr> 262 <tr> 263 <td width="27%"> 264 <p align="center"><font size="2">WarningStream</font></p> 265 </td> 266 <td width="73%"> 267 <p style="font-weight: medium"><font size="2">NOT CURRENTLY USED</font></p> 268 </td> 269 </tr> 270 <tr> 271 <td width="27%"> 272 <p align="center"><font size="2">WarningType</font></p> 273 </td> 274 <td width="73%"> 275 <p style="font-weight: medium"><font size="2">NOT CURRENTLY USED</font></p> 276 </td> 277 </tr> 278 <tr> 279 <td width="27%"> 280 <p align="center"><font size="2">WarningXServer</font></p> 281 </td> 282 <td width="73%"> 283 <p><font size="2">Warnings reported by the X11 subsystem.</font></p> 284 </td> 285 </tr> 286</table></ul> 287<p style="margin-bottom: 0in"><br /> 288</p> 289<p align="center" style="margin-bottom: 0in"><b>Error Sub-Classes</b></p> 290<ul><table width="100%" border="1" cellpadding="2" cellspacing="3"> 291 <col width="71*" /> 292 <col width="185*" /> 293 <tr> 294 <td width="28%"> 295 <p align="center"><font size="2"><b>Error</b></font></p> 296 </td> 297 <td width="72%"> 298 <p align="center"><font size="2"><b>Error Description</b></font></p> 299 </td> 300 </tr> 301 <tr> 302 <td width="28%"> 303 <p align="center"><font size="2">ErrorUndefined</font></p> 304 </td> 305 <td width="72%"> 306 <p><font size="2">Unspecified error type.</font></p> 307 </td> 308 </tr> 309 <tr> 310 <td width="28%"> 311 <p align="center"><font size="2">ErrorBlob</font></p> 312 </td> 313 <td width="72%"> 314 <p><font size="2">Error reported by BLOB I/O subsystem.</font></p> 315 </td> 316 </tr> 317 <tr> 318 <td width="28%"> 319 <p align="center"><font size="2">ErrorCache</font></p> 320 </td> 321 <td width="72%"> 322 <p><font size="2">Error reported by the pixel cache subsystem.</font></p> 323 </td> 324 </tr> 325 <tr> 326 <td width="28%"> 327 <p align="center"><font size="2">ErrorCoder</font></p> 328 </td> 329 <td width="72%"> 330 <p><font size="2">Error reported by coders (image format support).</font></p> 331 </td> 332 </tr> 333 <tr> 334 <td width="28%"> 335 <p align="center"><font size="2">ErrorConfigure</font></p> 336 </td> 337 <td width="72%"> 338 <p><font size="2">Errors reported while loading configuration files.</font></p> 339 </td> 340 </tr> 341 <tr> 342 <td width="28%"> 343 <p align="center"><font size="2">ErrorCorruptImage</font></p> 344 </td> 345 <td width="72%"> 346 <p><font size="2">Error reported when the image file is corrupt.</font></p> 347 </td> 348 </tr> 349 <tr> 350 <td width="28%"> 351 <p align="center"><font size="2">ErrorDelegate</font></p> 352 </td> 353 <td width="72%"> 354 <p><font size="2">Errors reported by the delegate (interface to 355 external programs) subsystem.</font></p> 356 </td> 357 </tr> 358 <tr> 359 <td width="28%"> 360 <p align="center"><font size="2">ErrorDraw</font></p> 361 </td> 362 <td width="72%"> 363 <p><font size="2">Error reported while drawing on image.</font></p> 364 </td> 365 </tr> 366 <tr> 367 <td width="28%"> 368 <p align="center"><font size="2">ErrorFileOpen</font></p> 369 </td> 370 <td width="72%"> 371 <p><font size="2">Error reported when the image file can not be 372 opened.</font></p> 373 </td> 374 </tr> 375 <tr> 376 <td width="28%"> 377 <p align="center"><font size="2">ErrorImage</font></p> 378 </td> 379 <td width="72%"> 380 <p><font size="2">Errors reported while drawing.</font></p> 381 </td> 382 </tr> 383 <tr> 384 <td width="28%"> 385 <p align="center"><font size="2">ErrorMissingDelegate</font></p> 386 </td> 387 <td width="72%"> 388 <p><font size="2">Error reported when an add-on library or program 389 is necessary in order to support the requested operation.</font></p> 390 </td> 391 </tr> 392 <tr> 393 <td width="28%"> 394 <p align="center"><font size="2">ErrorModule</font></p> 395 </td> 396 <td width="72%"> 397 <p><font size="2">Errors reported by the module loader subsystem.</font></p> 398 </td> 399 </tr> 400 <tr> 401 <td width="28%"> 402 <p align="center"><font size="2">ErrorMonitor</font></p> 403 </td> 404 <td width="72%"> 405 <p style="font-weight: medium"><font size="2">NOT CURRENTLY USED</font></p> 406 </td> 407 </tr> 408 <tr> 409 <td width="28%"> 410 <p align="center"><font size="2">ErrorOption</font></p> 411 </td> 412 <td width="72%"> 413 <p><font size="2">Error reported when an option is malformed or out 414 of range.</font></p> 415 </td> 416 </tr> 417 <tr> 418 <td width="28%"> 419 <p align="center"><font size="2">ErrorRegistry</font></p> 420 </td> 421 <td width="72%"> 422 <p><font size="2">Errors reported by the image/BLOB registry 423 subsystem.</font></p> 424 </td> 425 </tr> 426 <tr> 427 <td width="28%"> 428 <p align="center"><font size="2">ErrorResourceLimit</font></p> 429 </td> 430 <td width="72%"> 431 <p><font size="2">Error reported when a program resource is 432 exhausted (e.g. not enough memory).</font></p> 433 </td> 434 </tr> 435 <tr> 436 <td width="28%"> 437 <p align="center"><font size="2">ErrorStream</font></p> 438 </td> 439 <td width="72%"> 440 <p><font size="2">Errors reported by the pixel stream subsystem.</font></p> 441 </td> 442 </tr> 443 <tr> 444 <td width="28%"> 445 <p align="center"><font size="2">ErrorType</font></p> 446 </td> 447 <td width="72%"> 448 <p><font size="2">Errors reported by the type (font) rendering 449 subsystem.</font></p> 450 </td> 451 </tr> 452 <tr> 453 <td width="28%"> 454 <p align="center"><font size="2">ErrorXServer</font></p> 455 </td> 456 <td width="72%"> 457 <p><font size="2">Errors reported by the X11 subsystem.</font></p> 458 </td> 459 </tr> 460</table></ul> 461<p><br /><br /> 462</p> 463</div> 464</body> 465</html> 466