Magick++  7.1.2-29
Convert, Edit, Or Compose Bitmap Images
exceptions.cpp
1 // This may look like C code, but it is really -*- C++ -*-
2 //
3 // Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002, 2003
4 //
5 // Tests for throwing exceptions
6 //
7 
8 #include <Magick++.h>
9 #include <string>
10 #include <iostream>
11 
12 using namespace std;
13 
14 using namespace Magick;
15 
16 int main( int /*argc*/, char ** argv)
17 {
18  // Initialize ImageMagick install location for Windows
19  MagickPlusPlusGenesis genesis(*argv);
20 
21  int failures=0;
22 
23  cout << "Checking for working exceptions (may crash) ... ";
24  cout.flush();
25 
26  {
27  // Basic exception test
28  try
29  {
30  failures++;
31  throw int(100);
32  }
33  catch ( int /*value_*/ )
34  {
35  failures--;
36  }
37 
38  // Throw a Magick++ exception class.
39  try
40  {
41  failures++;
42  cout << "Throwing 'Magick::WarningResourceLimit' exception" << endl;
43  cout.flush();
44  throw WarningResourceLimit("How now brown cow?");
45  }
46  catch( Exception & /*error_*/ )
47  {
48  cout << "Successfully caught 'Magick::WarningResourceLimit' exception" << endl;
49  cout.flush();
50  failures--;
51  }
52 
53  // A more complex test
54  try
55  {
56  size_t columns = 640;
57  size_t rows = 480;
58  Geometry geometry(columns,rows);
59  Color canvasColor( "red" );
60  Image image( geometry, canvasColor);
61 
62  {
63  try
64  {
65  failures++;
66  cout << "Throwing library 'Magick::Exception' exception" << endl;
67  cout.flush();
68  image.directory();
69  }
70  catch ( Exception& /*error_*/ )
71  {
72  cout << "Successfully caught library 'Magick::Exception' exception" << endl;
73  cout.flush();
74  failures--;
75  }
76  }
77 
78  }
79  catch( Exception &error_ )
80  {
81  cout << "Bogus catch: Caught exception: " << error_.what() << endl;
82  cout.flush();
83  return 1;
84  }
85  catch( exception &error_ )
86  {
87  cout << "Bogus catch: Caught exception: " << error_.what() << endl;
88  cout.flush();
89  return 1;
90  }
91 
92  if ( failures )
93  {
94  cout << failures << " failures" << endl;
95  cout.flush();
96  return 1;
97  }
98  cout << "Exception testing passed!" << endl;
99  }
100 
101  return 0;
102 }