1、EE4E. C+ ProgrammingContentsIntroductionAn object oriented approach to exception handlingclass myClasspublic: int readFile(.)doif (!end_of_file)/ read the fileelsereturn 1; while not_end_of_file return number_of_bytes_read;The try-catch model of exception handlingApplicationObject 1Method callError
2、conditiontry clausecatch clauseException handling example#include #include class DivideByZeroException : public exceptionpublic:DivideByZeroException:DivideByZeroException(): exception(Attempted divide by zero) ;double quotient(int num, int den)if (den=0)throw DivideByZeroException();return (double)
3、(num)/den;int main()int number1,number2;double result;cout number1 number2)try result=quotient(number1,number2);cout The quotient is result endl;catch(DivideByZeroException &dzException)cout Exception! dzException.what() endl;cout nEnter two integers : ;return 0;Re-throwing exceptionsmainObject 2Obj
4、ect 1 throws exceptionCalls method ofCalls method ofre-throwhandles exceptionre-throwclass MyClass1 public:MyClass1() void aMethod1()trythrow exception();catch (exception &caughtException)cout Exception thrown in aMethod1 endl;throw;class MyClass2 public:MyClass2() void aMethod2()MyClass1 myObject1;
5、trymyObject1.aMethod1();catch(exception &caughtException)cout Exception re-thrown in aMethod2 endl;throw;int main()MyClass2 myObject2;trymyObject2.aMethod2();catch(exception &caughtException)cout Exception handled in main endl;cout “Program terminates ;return 0;Exception thrown in aMethod1Exception
6、re-thrown in aMethod2Exception handled in mainProgram terminatesException specificationsint aFunction(int arg)throw (ExceptionA, ExceptionB, ExceptionC) / function body)int aFunction(int arg) / function body)int aFunction(int arg)throw() / function body)int aFunction(int arg) throws IOException / Ja
7、va / function body)Multi-threading in C+A simple C+ Thread classclass Thread private:string m_strName;public:Thread();Thread(const char* nm);virtual Thread();void setName(const char* nm);string getName() const;void start();virtual void run();void sleep(long ms);void suspend();void resume();void stop
8、();void setPriority(int p);bool wait(const char* m,long ms=5000);void release(const char* m);/ Thread prioritiesstatic const int P_HIGHEST;static const int P_LOWEST;static const int P_NORMAL;Creating a simple multi-threaded application#include #include using namespace std;#include ou_thread.husing n
9、amespace openutils;class MyThread : public Threadprivate:int m_nCount;public:MyThread(int n,const char* nm)Thread:setName(nm);m_nCount=n;void run()for (int i=0; im_nCount; i+)cout getName().c_str() : i start();t2-start();t1-stop();t2-stop();catch(ThreadException ex)cout ex.getMessage().c_str();delet
10、e t1;delete t2;return 0;Thread synchronizationUnsynchronised threadsThread 1Thread 2Update databaseRead databasePre-emptPre-emptSynchronised threadsThread 1Thread 2Update databaseRead databasevoid MyThread:run()wait(MyMutex);for (int i=0; im_nCount; i+)cout getName().c_str() : i start();t2-start();t1-stop();t2-stop();m.release();catch(ThreadException ex)cout start();t1-setPriority(Thread:P_LOWEST);t2-start();t2-setPriority(Thread:P_HIGHEST);t1-stop();t2-stop();catch(ThreadException ex)cout ex.getMessage().c_str();delete t1;delete t2;return 0;And finally.