image.png

PPT:

image.png
image.png


image.png
image.png
image.png

代码:

image.png

  1. #ifndef EXCEPTION_H
  2. #define EXCEPTION_H
  3. namespace DJLib
  4. {
  5. #define THROW_EXCEPTION(e,m) (throw e(m,__FILE__,__LINE__))
  6. class Exception//析构函数为纯虚函数,说明这个类为抽象类,抽象类不能生成对象
  7. {
  8. protected:
  9. char* m_message;//当前异常的具体信息
  10. char* m_location;//异常的具体位置
  11. void init(const char* message, const char* file, int line);
  12. public:
  13. Exception(const char* message);
  14. Exception(const char* file, int line);
  15. Exception(const char* message,const char* file,int line);
  16. Exception(const Exception& e);
  17. Exception& operator= (const Exception& e);
  18. virtual const char* message() const;
  19. virtual const char* location() const;
  20. virtual ~Exception() = 0; //(纯虚函数不需要提供实现,需等子类来完成),但析构函数需要实现
  21. };
  22. class ArithmeticException : public Exception//计算异常
  23. {
  24. public:
  25. ArithmeticException():Exception(0){}
  26. ArithmeticException(const char* message) :Exception(message){}
  27. ArithmeticException(const char* file, int line):Exception(file,line){}
  28. ArithmeticException(const char* message,const char* file,int line):Exception(message,file,line){}
  29. ArithmeticException(const ArithmeticException& e):Exception(e){}
  30. ArithmeticException& operator = (const ArithmeticException& e)
  31. {
  32. Exception::operator =(e);
  33. return *this;
  34. }
  35. };
  36. class NullPointerException : public Exception//空指针异常 NullPointerException
  37. {
  38. public:
  39. NullPointerException():Exception(0){}
  40. NullPointerException(const char* message) :Exception(message){}
  41. NullPointerException(const char* file, int line):Exception(file,line){}
  42. NullPointerException(const char* message,const char* file,int line):Exception(message,file,line){}
  43. NullPointerException(const NullPointerException& e):Exception(e){}
  44. NullPointerException& operator = (const NullPointerException& e)
  45. {
  46. Exception::operator =(e);
  47. return *this;
  48. }
  49. };
  50. class IndexOutOfBoundsException : public Exception//越界异常 IndexOutOfBoundsException
  51. {
  52. public:
  53. IndexOutOfBoundsException():Exception(0){}
  54. IndexOutOfBoundsException(const char* message) :Exception(message){}
  55. IndexOutOfBoundsException(const char* file, int line):Exception(file,line){}
  56. IndexOutOfBoundsException(const char* message,const char* file,int line):Exception(message,file,line){}
  57. IndexOutOfBoundsException(const IndexOutOfBoundsException& e):Exception(e){}
  58. IndexOutOfBoundsException& operator = (const IndexOutOfBoundsException& e)
  59. {
  60. Exception::operator =(e);
  61. return *this;
  62. }
  63. };
  64. class NoEnoughMemoryException : public Exception//内存不足异常 NoEnoughMemoryException
  65. {
  66. public:
  67. NoEnoughMemoryException():Exception(0){}
  68. NoEnoughMemoryException(const char* message) :Exception(message){}
  69. NoEnoughMemoryException(const char* file, int line):Exception(file,line){}
  70. NoEnoughMemoryException(const char* message,const char* file,int line):Exception(message,file,line){}
  71. NoEnoughMemoryException(const NoEnoughMemoryException& e):Exception(e){}
  72. NoEnoughMemoryException& operator = (const NoEnoughMemoryException& e)
  73. {
  74. Exception::operator =(e);
  75. return *this;
  76. }
  77. };
  78. class InvalldParameterException : public Exception//参数错误异常 InvalldParameterException
  79. {
  80. public:
  81. InvalldParameterException():Exception(0){}
  82. InvalldParameterException(const char* message) :Exception(message){}
  83. InvalldParameterException(const char* file, int line):Exception(file,line){}
  84. InvalldParameterException(const char* message,const char* file,int line):Exception(message,file,line){}
  85. InvalldParameterException(const InvalldParameterException& e):Exception(e){}
  86. InvalldParameterException& operator = (const InvalldParameterException& e)
  87. {
  88. Exception::operator =(e);
  89. return *this;
  90. }
  91. };
  92. }
  93. #endif // EXCEPTION_H

image.png

  1. #include "exception.h"
  2. #include <cstring>
  3. #include <stdlib.h>
  4. using namespace std;
  5. namespace DJLib {
  6. void Exception::init(const char* message, const char* file, int line)
  7. {
  8. m_message = strdup(message);//拷贝信息到堆空间,m_message指向堆空间的信息
  9. if(file != NULL)
  10. {
  11. char sl[16] = {0}; //定义一个字符数组来接收转换的字符串
  12. itoa(line,sl,10);//发生异常的行号转换为字符
  13. m_location = static_cast<char *>(malloc(strlen(file)+strlen(sl)+2));//堆空间申请内存
  14. m_location = strcpy(m_location,file);
  15. m_location = strcat(m_location,":"); //拼接
  16. m_location = strcat(m_location,sl);
  17. }
  18. else
  19. {
  20. m_location = NULL;
  21. }
  22. }
  23. Exception::Exception(const char* message)
  24. {
  25. init(message,NULL,0);
  26. }
  27. Exception::Exception(const char* file, int line)
  28. {
  29. init(NULL,file,line);
  30. }
  31. Exception::Exception(const char* message,const char* file,int line)
  32. {
  33. init(message,file,line);
  34. }
  35. Exception::Exception(const Exception& e)//拷贝构造函数,深拷贝
  36. {
  37. m_message = strdup(e.m_message);
  38. m_location = strdup(e.m_location);
  39. }
  40. Exception& Exception::operator= (const Exception& e)//赋值操作符
  41. {
  42. if(this != &e)
  43. {
  44. free(m_message);
  45. free(m_location);
  46. m_message = strdup(e.m_message);
  47. m_location = strdup(e.m_location);
  48. }
  49. return *this;
  50. }
  51. const char* Exception::message() const
  52. {
  53. return m_message;
  54. }
  55. const char* Exception::location() const
  56. {
  57. return m_location;
  58. }
  59. Exception::~Exception()
  60. {
  61. free(m_message);
  62. free(m_location);
  63. }
  64. }

image.png

  1. #include <iostream>
  2. #include "exception.h"
  3. #include "smartpointer.h"
  4. using namespace std;
  5. using namespace DJLib;
  6. int main()
  7. {
  8. try
  9. {
  10. // throw Exception("test" ,__FILE__,__LINE__);
  11. THROW_EXCEPTION(InvalldParameterException,"test");
  12. }
  13. catch(const ArithmeticException& e)//子类放上面,父类放下面
  14. {
  15. cout<<" catch(const ArithmeticException(exception& e))" << endl;
  16. cout<< e.message() << endl;
  17. cout<< e.location() << endl;
  18. }
  19. catch(const NullPointerException& e)//子类放上面,父类放下面
  20. {
  21. cout<<" catch(const NullPointerException(exception& e))" << endl;
  22. cout<< e.message() << endl;
  23. cout<< e.location() << endl;
  24. }
  25. catch(const IndexOutOfBoundsException& e)//子类放上面,父类放下面
  26. {
  27. cout<<" catch(const IndexOutOfBoundsException(exception& e))" << endl;
  28. cout<< e.message() << endl;
  29. cout<< e.location() << endl;
  30. }
  31. catch(const NoEnoughMemoryException& e)//子类放上面,父类放下面
  32. {
  33. cout<<" catch(const NoEnoughMemoryException(exception& e))" << endl;
  34. cout<< e.message() << endl;
  35. cout<< e.location() << endl;
  36. }
  37. catch(const InvalldParameterException& e)//子类放上面,父类放下面
  38. {
  39. cout<<" catch(const InvalldParameterException(exception& e))" << endl;
  40. cout<< e.message() << endl;
  41. cout<< e.location() << endl;
  42. }
  43. catch(const Exception& e)//子类放上面,父类放下面
  44. {
  45. cout<<" catch(const Exception(exception& e))" << endl;
  46. cout<< e.message() << endl;
  47. cout<< e.location() << endl;
  48. }
  49. return 0;
  50. }