Chapter 5: Exception class
In this chapter, we will focus on the Exception class, to identify errors in the Magick++ function calls at run time, ensuring easier debugging of code. We will compliment it with example program that will aid in the understanding of its use.
Introduction The Exception class is derived from C++ exception class. It is called using try/catch block. The exception in Magick++ can be 1. Warnings, which allows programs to continue its running but could result in erroneous output or 2. Errors, which will terminate the program. Some errors like file read error can be fixed with user intervention.
The try/catch block can be placed across single Magick++ function call or across a block of code. Here is an example that illustrates the use of try/catch to determine whether the read operation was performed successfully.
1. //Instruction for compiling 2. //Compiling - c++ -o try_catch try_catch.cpp `Magick++-config --cppflags --cxxflags --ldflags --libs` 3. //Executing - try_catch infile.ext outfile.ext 4. 5. #include 6. #include 7. using namespace std; 8. using namespace Magick; 9. 10. int main(int argc,char **argv){ 11. 12. //If all arguments are not supplied, print error 13. if(argc<3){ 14. printf("Usage: try_catch infile.ext outfile.ext\n"); 15. exit(0); 16. } 17. 18. //Create the class and read the file 19. Image master; 20. 21. //Read the inputfile. If the file does not exist, throw an exception and quit 22. try{ 23. master.read(argv[1]);
24. } 25. catch(ErrorFileOpen &error){ 26. cerr << "Error: " << error.what() << endl; 27. } 28. 29. //Display image. If the display system does not work, throw an exception 30. try{ 31. master.display(); 32. } 33. catch(Error &error){ 34. cerr << "Error: " << error.what() << endl; 35. } 36. 37. //Write the file to a different format 38. //If there is any error in writing the file, throw an exception 39. try{ 40. master.write(argv[2]); 41. } 42. catch(Error &error){ 43. cerr << "Error: " << error.what() << endl; 44. } 45. return 0; 46. }
Prog4: try_catch.cpp: This program checks for error in reading and writing a file.
The first try/catch block (Lines 22-27) is placed across the read function to determine any error in reading such as missing file. The second try/catch block (Lines 30-35) is placed against the display function to determine any error in the Xwindow system rendering. The third try/catch block (Lines 39-44) is placed against the write command to report error in writing file due to insufficient space, permissions etc. These try/catch blocks can also be combined to reduce the amount of code in the program and the level of sophistication of the program. With this background, we will look at all other classes in the subsequent chapter. Due to constrains in having large programs inside the text of this tutorial, we will not be performing error and warning checks in some of the subsequent programs. But for good programming practice, exception have to be thrown and checked. In the next chapter, we will focus on performing segmentation using Magick++ using Adaptive thresholding and Fuzzy C Means clustering algorithm. Each of these algorithms will be briefly introduced and the corresponding programs will be discussed.