gpt4 book ai didi

c++ - 无法捕获异常

转载 作者:行者123 更新时间:2023-11-28 07:03:58 24 4
gpt4 key购买 nike

这是完整的代码:TomTimers::set() 会抛出异常,但 tom_timing_test.cpp 中的 try/catch block 似乎没有捕获到它。当我运行它时,我得到这个:

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;
}
}

最佳答案

好吧,现在,当您捕获为 exception 时,抛出的类必须是 exception 或具有 exception 作为可访问(在本例中表示 public)基类。

关于c++ - 无法捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22048213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com