, Teach Yourself Visual C 6 in 21 days 

[ Pobierz całość w formacie PDF ]
.If you want to raise this exception, use AfxThrowNotSupportedException(),which doesn t required any parameters.There are also no extended members or functionsassociated with this exception it just means unsupported.Using the User ExceptionYou can use the CUserException class to generate application-specific exception objects.You might want to do this when your program is interacting with the user to halt theprocess should she choose a certain option.For example, when you are using theAppWizard, you can press Esc at any time to cancel the whole process.Microsoft mighthave used CUserException to do this by detecting the Esc key and then raising a userexception object.This exception can be raised by a call to the AfxThrowUserException() function andthen caught in the usual try and catch blocks.There are some places in the MFC wherethis exception is raised, such as during dialog box validation or if the file is too big foran edit view. 032 31240-9 APP D 4/27/00 1:08 PM Page 675Understanding and Exception Handling 675Generating Your Own Custom Exception ClassesYou can derive your own exception classes from CException and add your specificextended functionality.Listing D.6 shows the class definition for such a custom excep-tion class that extends the normal functionality by adding a m_strMessage CString vari-able to the exception, enabling you to specify your own message when constructing theexception.LISTING D.6.LST29_6.CPP CLASS DEFINITION FOR CCustomException IMPLEMENTED INCustomException.h.1: // ** CustomException.h2: // ** Header file for CCustomException3:4: class CCustomException : public CException5: {6: DECLARE_DYNAMIC(CCustomException);7:8: public:9: CCustomException(CString strMessage);10:11: CString m_strMessage;D12: };In Listing D.6 the class is implemented in its own CustomException.h header file andderives from CException in line 4.The DECLARE_DYNAMIC macro in line 6 supplies theMFC CObject-derived runtime class information required for you to decide the excep-tion type in a catch-all catch block.The constructor definition in line 9 takes a CStringstrMessage parameter to let you create the custom exception with the message that willbe stored in the m_strMessage CString variable declared in line 11.The corresponding CCustomException class implementation is shown in Listing D.7.LISTING D.7.LST29_7.CPP IMPLEMENTATION OF THE CCustomException CLASS.1: // ** CustomException.cpp2: // ** Implementation for CCustomException exception3:4: #include  stdafx.h5: #include  CustomException.h6:7: IMPLEMENT_DYNAMIC(CCustomException,CException);8:9: CCustomException::CCustomException(CString strMessage)10: : m_strMessage(strMessage)11: {12: } 032 31240-9 APP D 4/27/00 1:08 PM Page 676676 Appendix DIn Listing D.7 the usual header files are included, and the IMPLEMENT_DYNAMIC macro isused in line 7 to implement the MFC runtime class information functions.The construc-tor in line 9 takes the strMessage parameters and initializes the m_strMessage membervariable with this string value in line 10.You can then use the custom exception class in your application, as shown in ListingD.8.LISTING D.8.LST29_8.CPP USING THE NEW CCustomException CLASS.1: try2: {3: // ** Something goes wrong4: CCustomException* pCustomEx =5: new CCustomException( My custom error occured );6: throw(pCustomEx);7: }8: catch(CCustomException* e)9: {10: // ** Access the extended m_strMessage string11: AfxMessageBox(e->m_strMessage);12: e->Delete();13: }In Listing D.8 a new CCustomException object is created with the application-specificerror text in lines 4 and 5 and is thrown in line 6.This is then caught by the catch key-word in line 8 and the custom information used by the message box in line 11.Theexception is then deleted in line 12.If you try this, remember that the implementation code must also have an #include forthe CustomException.h header file to retrieve the class definition like this:#include  CustomException.h 033 31240-9 APP E 4/27/00 1:11 PM Page 677APPENDIX EUsing the Debugger andProfilerby Jon BatesCreating Debugging and BrowseInformationA large part of application development is actually debugging your program.All software development is a tight cycle of application design, implementa-tion, and debugging.Visual C++ has an extensive debugging environment and a range of debuggingtools that really help with program development.You can quickly identifyproblems, watch the contents of variables, and follow the flow of programsthrough your own code and the MFC code.Tools such as the Spy++ program can show you the messages passed betweenWindows and your application and let you spy on applications to see whichuser interface controls and Window styles they use. 033 31240-9 APP E 4/27/00 1:11 PM Page 678678 Appendix EUsing Debug and Release ModesThere are two main compiler configurations that you can set to build your application:Debug and Release mode.You can change these modes by clicking the Project menu andselecting the Settings option or by pressing Alt+F7, which will display the ProjectSettings dialog box (see Figure E.1).The main project settings are shown at the top leveland can be changed by selecting the options listed in the combo box.When one setting isselected, changes that you make to any options on the tabs on the right will be set againstthat configuration.When you build the application, it will be built using your currentconfiguration settings, or you can select All Configurations to build and make changes toall configurations simultaneously.FIGURE E.1.The C/C++ tab of theProject Settings dialogbox.Both Release and Debug configurations are supplied whenever you create a new project;they produce very different object code.When configured for Debug mode, your buildwill produce a large and fairly slow executable program [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • anikol.xlx.pl