2014-02-26 44 views
0

這是全碼:TomTimers ::集()將拋出一個異常,但在tom_timing_test.cpp的try/catch塊似乎沒有抓住它。當我運行它,我得到這個:無法捕捉異常

terminate called after throwing an instance of 'TomTimers_Exception' 
Aborted (core dumped) 

tom_timing_test.h

#ifndef TOM_TIMING_TEST_H 
#define TOM_TIMING_TEST_H 

#include <time.h> 
#include <sys/time.h> 
#include <utility> 

typedef struct timespec timespec_t; 

#define ORWL_NANO (+1.0E-9) 
#define ORWL_GIGA UINT64_C(1000000000) 

// ============================================================================= 
#ifdef __MACH__ // support for OS X 
// ============================================================================= 

#include <mach/clock.h> 
#include <mach/mach.h> 
#include <mach/mach_time.h> 

// timebase is a scaling factor that converts the difference between 
// two mach_absolute_times into seconds. 
static double orwl_timebase = 0.0; 
static uint64_t orwl_timestart = 0; 

timespec_t porta_gettime(void) { 
    if (!orwl_timebase) { 
    mach_timebase_info_data_t tb; 
    mach_timebase_info(&tb); 
    orwl_timebase = tb.numer; 
    orwl_timebase /= tb.denom; 
    orwl_timestart = mach_absolute_time(); 
    } 
    timespec_t t; 
    double diff = (mach_absolute_time() - orwl_timestart) * orwl_timebase; 
    t.tv_sec = diff * ORWL_NANO; 
    t.tv_nsec = diff - (t.tv_sec * ORWL_GIGA); 
    return t; 
} 

// ============================================================================= 
#else // support for Linux 
// ============================================================================= 

// porta_gettime() - Get real time as a timespec_t 
// timespec{sec, nsec} 
timespec_t porta_gettime(void) { 
    timespec_t tnow; 
    clock_gettime(CLOCK_REALTIME, &tnow); 
    return tnow; 
} 

#endif 
// ============================================================================= 


#include <string> 
#include "wd_sprintf.h" 
#include <iostream> 
#include <set> 
#include <boost/foreach.hpp> 
#include <boost/unordered_map.hpp> 

using std::set; 
using std::cout; 
using std::endl; 
using std::string; 
using boost::unordered_map; 


typedef struct { 
    double avg_onset; 
    double avg_duration; 
    long samples; 
} Average; 

typedef unordered_map<string, Average>  Average_Map; 
typedef unordered_map<string, timespec_t> Timer_Map; 

static timespec_t t0;  // absolute timebase 
static const Average init_avg {0,0,0}; 

class TomTimers_Exception : std::runtime_error { 
public: 
    TomTimers_Exception(string const& msg) : std::runtime_error(msg) {} 
}; 

class TomTimers { 
public: 
    TomTimers() { 
    t0 = porta_gettime(); // set an absolute timebase 
    } 

    void set() { 
     if (!_timer_map.empty()) { 
      // There are still running timers. Usage error. 
      throw TomTimers_Exception("TomTimers::set() called with active timers."); 
     } 
     _cycle = porta_gettime(); // set the start-of-cycle time 
    } 

    void start(const string& id) { 
    timespec_t now = porta_gettime(); // get real time 
    start(id, now); 
    } 
    void start(const string& id, timespec_t time) { 
    _timer_map[id] = time; 
    _average_map.insert(std::make_pair(id, init_avg)); 
    } 

    timespec_t stop(const string& id) { 
     timespec_t ts_start = _timer_map[id]; 
     timespec_t ts_stop = porta_gettime(); 
     Average previous = _average_map[id]; 
     timespec_t tdur; 

     // Compute this duration and update avg duration 
     tdur.tv_sec = ts_stop.tv_sec - ts_start.tv_sec; // can't be neg 
     tdur.tv_nsec = ts_stop.tv_nsec - ts_start.tv_nsec; // might be neg 
    double new_duration = tdur.tv_sec * ORWL_GIGA + tdur.tv_nsec; 
    _average_map[id].avg_duration = 
     (previous.avg_duration * previous.samples + new_duration) 
     /(_average_map[id].samples + 1); 

    // Compute this onset and update avg onset 
     tdur.tv_sec = ts_start.tv_sec - _cycle.tv_sec; // can't be neg 
     tdur.tv_nsec = ts_start.tv_nsec - _cycle.tv_nsec; // might be neg 
    double new_onset = tdur.tv_sec * ORWL_GIGA + tdur.tv_nsec; 
    _average_map[id].avg_onset = 
     (previous.avg_onset * previous.samples + new_onset) 
     /(_average_map[id].samples + 1); 

    ++(_average_map[id].samples); 

     _timer_map.erase(id); 
    return ts_stop; 
    } 

    // stop_start(id1, id2) 
    // 
    // Stops id1's timer and starts id2's timer at the same time. 
    void stop_start(const string& id_stop, const string& id_start) { 
     start(id_start, stop(id_stop)); 
    } 

    void dump() { 
    // Sort the names by copying them into an ordered set 
    // and why the FUCK do I have to say std:: ??? 
    std::set<string> ordered_set; 
    BOOST_FOREACH(Average_Map::value_type& i, _average_map) { 
     ordered_set.insert(i.first); 
    } 

     BOOST_FOREACH(const std::set<string>::value_type& v, ordered_set) { 
      cout << wd_sprintf("%s: Onset %12.8f Dur %12.8f [%d samples]", 
        v, 
        _average_map[v].avg_onset, 
        _average_map[v].avg_duration, 
        _average_map[v].samples) 
     << endl; 
     } 
    } 

private: 

    timespec_t _cycle; 
    Timer_Map _timer_map; 
    Average_Map _average_map; 

}; 


#endif 

tom_timing_test.cpp

#include "tom_timing_test.h" 

int 
main(int argc, char** argv) { 

    long repetitions; 
    if (argc > 1) { 
     repetitions = atoi(argv[1]); 
    } 
    else { 
     cout << "You need repetitions" << endl; 
     return 0; 
    } 

    TomTimers tt; 

    const string DUR_1 {"Phase 1"}; 
    const string DUR_2 {"Phase 2"}; 
    const string DUR_3 {"Phase 3"}; 

    try { 
     for (int i = 0; i < repetitions; ++i) { 
      tt.set(); 
      tt.start(DUR_1); 
      //  sleep(1); 
      tt.stop_start(DUR_1, DUR_2); 
      //  sleep(2); 
      tt.stop_start(DUR_2, DUR_3); 
      //  sleep(2);   
      // tt.stop(DUR_3); 
     } 

     tt.dump(); 
    } 
    catch (std::exception& e) { 
     cout << "Caught exception: " << e.what() << endl; 
    } 
} 

回答

1

孔U現在,當你發現爲exception然後拋出的類必須或者是exception或具有exception作爲訪問(在這種情況下,意味着public)基類。

+0

憐憫!我沉默忘了。謝謝'ee! – Chap

+0

有些章節編輯了語言(缺乏理解),並且爲了好的措施編輯了技術內容(缺乏理解),以便答案變得不正確和毫無意義。這是其他4人批准的。我現在只是回滾編輯,但請人們,**不要贊成正確性**,不要批准編輯,使答案不正確,無論新的spelling-corect vict mey縫多麼誘人和吸引力。 –

+0

不是這個Chap,當然... – Chap