There are something in exception handling that is not easy apprehensible for most readers, so here is the discussion on some specific of the exception handlings.
the code that the example below will use to illustrate is as follow.
#include <iostream>
#include <string>
using std::cout;
using std::cerr;
using std::endl;
using std::string;
class Excp
{
public :
// print error message
static void print(string msg) {
cerr << msg << endl;
}
};
class popOnEmpty : public Excp { };
class pushOnFull: public Excp {
public:
pushOnFull(int value ) {} ;
};
class iStack {
public:
void push(int value);
bool full() { return true ; }
};
so suppose that we have a throw expression as follow.
void iStack::push(int value)
{
if (full() )
{
// value stored in exception object
throw pushOnFull(value);
}
}
there are actually a lots of things happening underneath.
many stap take place as a consequence of executing this throw exception
- the throw exception creates a temporary object of class type pushOnFull by calling the class constructor
- An exception object of type pushOnFull is created to be passed to the exception handler. The exception object is is a copy of the temporary object created by the throw expression in step 1. it is created by calling the calss pushOnfull's copy constructor
- the temporary object created by the throw expression in step 1 is destroyed byfore the search for a handler starts.
why the exception object is necessary
you may wonder why the step 2 is needed and why an expression object is created -
the expression
pushOnFull(value);
create a temporary boject that is destroyed at the end of throw expression. The exception, however must last until a handler has been found, which may be many functions further up the chain
of the function calls. It is therefore necessary to copy the temporary object into a storage location, called the exception object, that is guaranteed to last until the exception hass ben handled
however, this a classic way how how an exception is constructed, but in some cases it may be possible for the implementation to create the exception object directly, without creating the temporary object in step 1, however, this temporary elinimation is not required or always possible.
another tripwire/pitfall/trap that people falls is when handling exception when pointer is concerned.
here is the code.
void iStack::push(int value)
{
if (full()) {
pushOnFull except(value);
Excp *pse = &except;
throw *pse; // exception object has type Excp
}
}
because of the discussion we had before, since the dereference will return the type of object what the pionter points to, so the return value is of type Excp... even though the pointer actually point to a more derived class object , the actualy type is not examined to create the exception object
restriction on the class that serve as a exception object
the implied restriction on the kind of classes that can be used to create exception objects, the throw exception in the iStack member function push() is in error if
- the class pushOnFull does not have a constructor that accepts an arguments of type int or if this constructor is not accessible
- the class pushOnFull has neither a copy constructor or the constructor is not acceesible
- the class pushOnFull is an abstract base class, because a program cannot create an object of an abstract class type .
分享到:
相关推荐
- **说明**:C++11及以后版本中,异常规范(throw-specification)已被弃用。 - **建议**: - 避免显式指定异常规格。 - 使用 noexcept 规格来表示函数不会抛出异常。 **5.7 Item 15:了解异常处理的系统开销** -...
根据提供的信息,《由浅入深学C++-基础、进阶与必做300题》这本书是由胡超编写的,旨在帮助读者逐步掌握C++编程语言的基础到高级知识,并通过大量的练习题来巩固所学内容。下面我们将从C++的基础知识、进阶技巧以及...
6. **异常处理(Exception Handling)**:通过try、catch和throw关键字,C++允许程序员捕获和处理运行时错误,保证程序的健壮性。 7. **STL(Standard Template Library)**:C++标准库中包含了一组模板容器(如...
On the other hand, you must include the header file for Foo if your class subclasses Foo or has a data member of type Foo. Sometimes it makes sense to have pointer (or better, scoped_ptr) members ...
4. **异常处理(Exception Handling)**:C++提供了异常处理机制,通过try、catch和throw关键字来捕获和处理运行时错误。 5. **标准库(Standard Library)**:C++的标准库包含了大量的容器(如vector、list、set等...
class MyException : public std::exception { public: const char* what() const noexcept override { return "A custom exception occurred."; } }; ``` ### 7. `noexcept`关键字 `noexcept`关键字用于声明一...
- try-catch-finally结构:`try { /* code that may throw an exception */ } catch (ExceptionType e) { /* handle the exception */ } finally { /* clean up resources */ }` 9. **标准模板库(STL)** - 容器:...
throw gcnew Exception("An error occurred while calling the native DLL."); } } private: [DllImport("yourdll.dll", CallingConvention = CallingConvention.Cdecl)] static extern int NativeAddNumbers...
7. **继承**:继承机制与传统的C++相似,可以声明公共或私有的基类,如`public ref class PluginRunException : public Exception`。 8. **重载**:函数和方法可以被重载,提供多个同名但参数列表不同的版本,如`...
1. **类(Class)**: C++中的类是面向对象编程的基础,它定义了一组数据和操作这些数据的方法。通过类,我们可以创建具有特定属性和行为的对象。类的设计需要遵循封装原则,将数据和方法隐藏在内部,仅通过公共接口与...
- C++中的异常处理机制允许程序在运行时检测并处理错误,使用`try`、`catch`和`throw`关键字。 - 异常类层次结构始于`std::exception`,自定义异常类通常从这个基类派生。 7. **内存管理和智能指针** - C++中的...
4. **异常处理(Exception Handling)**:通过try、catch和throw关键字,C++提供了处理运行时错误的方式。理解异常处理机制有助于编写健壮的代码。 5. **文件输入输出(File I/O)**:C++提供了fstream库进行文件...
6. 异常处理(Exception Handling):C++提供了异常处理机制,使得程序在遇到错误时能够优雅地恢复。通过分析代码,你可以了解try、catch和throw语句的用法,以及如何设计健壮的异常处理策略。 7. 输入/输出流(I/O...
3. **异常处理(Exception Handling)**:C++通过try、catch和throw关键字支持异常处理。飞鸽C++源代码中可能包含异常处理的实例,这有助于理解错误处理和程序健壮性的构建。 4. **STL(Standard Template Library...
2. **面向对象编程**:C++的核心特性之一就是其面向对象的特性,包括类(class)、对象(object)、封装、继承、多态等概念。这些内容在文档中会有详细的解释和实例。 3. **模板(Template)**:C++的模板允许创建...
C++还包括异常处理(Exception Handling),这是一种错误处理机制,允许程序在遇到问题时优雅地恢复,而不是突然崩溃。你将学习如何使用try、catch和throw关键字来捕获和处理异常。 最后,C++11、C++14和C++17等新...
- 类(Class):C++的核心是类,它定义了一组数据和操作这些数据的方法。 - 对象(Object):类的实例,具有类定义的属性和行为。 - 封装(Encapsulation):通过类隐藏内部实现细节,只暴露必要的接口。 - 继承...
- **异常处理**(Exception Handling):包括`try`, `catch`, `throw`等关键字,用于处理运行时错误。 #### 4. 类与对象 - **类定义**(Class Definition):定义了一个新的数据类型,包含数据成员(属性)和成员...
在C++编程语言中,虚数计算涉及到复数的概念,这是数学中的一个重要分支。复数由实部和虚部组成,通常表示为a + bi的形式,其中a是实部,b是虚部,i是虚数单位,满足i² = -1。在C++中,我们可以自定义复数类来实现...