【翁恺】34-异常语句

try blocks

  • try blocks

    try {...}
        catch ...
        catch ...
  • establishes any number of handers

  • not needed if you don't use any handlers

  • shows where you expert to handle exceptions

  • const cycles

exception handlers

  • select exception by type

  • can re-raise exceptions

  • two forms

    catch (SomeType v){}//handler code  很像函数
    catch(...){}//handler code  处理所有异常,但是得不到异常对象
  • take a single argument(like a formal parameter)

selecting a handler

  • can have any number of handlers

  • handlers are checked in order of appearance  针对每一个catch做下面的 1, 2, 3

    1. check for exact match
    2. apply base class conversions
      • reference and pointer type,only
    3. ellipses(...) match all

    inheritance can be used to structure exceptions

example:using inheritance

  • hierarchy of exception types

    class MathErr{
        ...
        virtual void diagnstic();
    };
    class OverflowErr:pubic MathErr{...}
    class UnderflowErr:pubic MathErr{...}
    class ZeroDivideErr:pubic MathErr{...}

using handlers

try{
    //code to expercise math options 
    throw UnderFlowErr
}catch(ZeroDivideErr& e){
    //handle zero divide cade
}catch(MathErr& e){
    //handle other math errors
}catch(...){
    //any other exceptions
}

exception specifications

  • declare which exceptions function might raise

  • part of function prototypes

    void abc(int a):throw(MathErr){ //约束abc只抛出MathErr异常
        ...
    }
  • not checked at compile time

  • at run time

    • if an exceptions not in the list propagates out,the "unexpected" exception is raised

examples

Printer::print(Document&):throw(PrinterOfffLine,BadDocument){
    ...
    PintManager::print(Document&):throw (BadDocument){
        //raises or doesn't handle BadDocuement
    	}
    void  googguy():throw(){ // 什么都不抛出
        //handle all exceptions
    }
    voi average(){}//no spec, no checking
}

exceptions and new

  • new does NOT returned 0 on failure

  • new raises a bad_alloc() exception

    void func(){
        try{
            while(1){
                char*p = new char[10000];
            }
        }catch(bad_alloc& e)
     }	
    }

failure in constructors:  构造函数中出现异常

  • no return value is possible
  • use an "uninitialized flag"
  • defer work to an init() function

better throw an exception

if you constructor can't complete,throw an exception  构造没有完成,析构不能调用

  • dtors for objects whose ctor didn't complete won't be called
  • clean up allocated resources before throwing  
#include<iostream>
using namespace std;
 
 
class A 
{
public:
	A() { buf = new char[1024]; delete this; throw 0; }
	~A() { delete buf; }
};
 
 
int main()
{
	A a;//析构没做,但是空间已经收回了,如果析构里有delete相关的,也不能收回空间,a本身不是垃圾,但buf是
	A* p = new A();//构造函数没有成功执行,运行不到delete回收空间
	delete p;
	return 0;
}
//main.cpp

programming with exceptions

prefer catching exceptions by reference

  • throwing/catching by value involves slicing

    struct X{}
    struct Y:pubic X{}
    try{
        throw Y():
    }catch(X x){
        //was it or Y?
    }
  • throwing/catching by pointer introduces coupling between normal and handler code:

    try{
        throw new Y();
    }catch(Y* p){
        //whoop ,forgot to delete
    }

catch exceptions by reference:   推荐做法

struct B{
    virtual void print(){...}
}
struct D : public B{...};

try {
    throw D("D error");
}
catch(B& b){
    b.print() // print D's error
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

理心炼丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值