我最近遇到了这个语法try-catchfor function.
struct A
{
int a;
A (int i) : a(i) // normal syntax
{
try {}
catch(...) {}
}
A () // something different
try : a(0) {}
catch(...) {}
void foo () // normal function
try {}
catch(...) {}
};
Run Code Online (Sandbox Code Playgroud)
两种语法都有效.除了编码风格之外,这些语法之间是否有任何技术差异?在任何方面,语法之一是否优于其他语法?
我想知道程序员何时使用函数try块.什么时候有用?
void f(int i)
try
{
if ( i < 0 )
throw "less than zero";
std::cout << "greater than zero" << std::endl;
}
catch(const char* e)
{
std::cout << e << std::endl;
}
int main() {
f(1);
f(-1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:(在ideone处)
greater than zero
less than zero
Run Code Online (Sandbox Code Playgroud)
编辑:因为有些人可能认为函数定义的语法不正确(因为语法看起来不熟悉),我要说它不是不正确的.它叫做function-try-block.参见C++标准中的§8.4/ 1 [dcl.fct.def].
此代码int在构造Dog类中的对象时抛出异常UseResources.该int异常由正常捕获try-catch块和代码输出:
Cat()
Dog()
~Cat()
Inside handler
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
using namespace std;
class Cat
{
public:
Cat() { cout << "Cat()" << endl; }
~Cat() { cout << "~Cat()" << endl; }
};
class Dog
{
public:
Dog() { cout << "Dog()" << endl; throw 1; }
~Dog() { cout << "~Dog()" << endl; }
};
class UseResources
{
class Cat cat;
class Dog dog;
public:
UseResources() : …Run Code Online (Sandbox Code Playgroud) 在具有多个成员的类的成员初始化期间,似乎希望能够捕获由任何特定成员初始化程序生成的异常,以包装在额外的上下文中以便重新抛出,但是函数 try 块的语法没有出现为了适应这一点。
#include <stdexcept>
#include <string>
#include <sstream>
using namespace std::literals::string_literals;
[[noreturn]]
int thrower() { throw std::runtime_error("unconditional throw"s); }
int nonThrower() { return 3; }
class C {
int x;
int y;
public:
C();
};
class XError : public std::runtime_error {
public:
XError(const std::string& what) : std::runtime_error((std::stringstream() << "xerror: "s << what).str()) {};
};
class YError : public std::runtime_error {
public:
YError(const std::string& what) : std::runtime_error((std::stringstream() << "yerror: "s << what).str()) {};
};
C::C() try:
x(nonThrower()),
y(thrower()) {}
catch(const …Run Code Online (Sandbox Code Playgroud) 我在C++中创建一个简单的线程服务器应用程序,事实是,我使用libconfig ++来解析我的配置文件.好吧,libconfig不支持多线程,因此我使用两个包装类来完成"支持".点是,其中一个失败:
class app_config {
friend class app_config_lock;
public:
app_config(char *file) :
cfg(new libconfig::Config()),
mutex(new boost::mutex())
{
cfg->readFile(file);
}
private:
boost::shared_ptr<libconfig::Config> cfg;
boost::shared_ptr<boost::mutex> mutex;
};
Run Code Online (Sandbox Code Playgroud)
从我的main.cpp文件调用时失败可怕:
app_main::app_main(int c, char **v) : argc(c), argv(v) {
// here need code to parse arguments and pass configuration file!.
try {
config = app_config("mscs.cfg");
} catch (libconfig::ParseException &e) {
cout << "Parse error at line " << e.getLine() << ": " << e.getError() << endl;
throw;
} catch (libconfig::FileIOException &e) {
cout << "Configuration …Run Code Online (Sandbox Code Playgroud) c++ construction exception-handling member-variables function-try-block
我想知道是否(以及如何)捕获成员析构函数中抛出的异常.例:
#include <exception>
class A
{
public:
~A() {
throw std::exception("I give up!");
}
};
class B
{
A _a;
public:
~B() {
// How to catch exceptions from member destructors?
}
};
Run Code Online (Sandbox Code Playgroud)