我对返回的字符串有一些问题:我有一个返回字符串的函数:
\n\nstd::string foo(const std::string& paramIn)\n{\n return ("My message" + paramIn);\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我有一个异常类:
\n\nclass MyException : public std::exception\n{\nprivate:\n std::string m_msg;\n\npublic:\n MyException(std::string& msg) : m_msg(msg) {}\n virtual ~MyException() throw() {}\n\n virtual const char* what() const throw()\n {\n return m_msg.c_str();\n }\n};\nRun Code Online (Sandbox Code Playgroud)\n\n当我抛出一个MyException以作为参数的foo函数返回
throw MyException(foo("blabla"));\nRun Code Online (Sandbox Code Playgroud)\n\n它说:
\n\n/home/me/my_proj/main.cpp: In member function \xe2\x80\x98std::string ConfigFile::getClassifierExtractorType()\xe2\x80\x99:\n/home/me/my_proj/main.cpp:79:96: error: no matching function for call to \xe2\x80\x98MyException::MyException(std::string)\xe2\x80\x99\n throw MyException(foo("blabla"));\n ^\n/home/me/my_proj/main.cpp:79:96: note: candidates are:\nIn file included from /home/me/my_proj/main.hpp:5:0,\n from /home/me/my_proj/main.cpp:1:\n/home/me/my_proj/CMyExceptions.hpp:38:3: note: MyException::MyException(std::string&)\n …Run Code Online (Sandbox Code Playgroud) 所以,想象一下我有这个代码:
typedef struct Point {
float x;
float y;
} Point;
class Foo {
private:
Point * p;
public:
Foo () {
this->p = (Point *) malloc(sizeof(Point));
if (this->p == NULL) {
// throw exception_malloc_fail;
}
}
};
Run Code Online (Sandbox Code Playgroud)
一旦 malloc 在构造函数内分配内存失败,我应该抛出哪种异常?
在这种情况下,我不能简单地 returnfalse或NULL。所以throw声明应该是要走的路。
但是,我找不到要抛出的正确类型的异常。我应该抛出一个默认异常吗?或者有没有适合这种情况的?
我尝试用谷歌搜索它,但所有结果都是关于 C++ 的throw std::exception()。
我正在阅读库(很好奇像printf,malloc和 之类的东西FILE是如何实现的)并发现了该malloc函数的定义:
extern void *malloc (size_t __size) __THROW __attribute_malloc__
__attribute_alloc_size__ ((1)) __wur;
Run Code Online (Sandbox Code Playgroud)
当使用IDE(Visual Studio Code)追溯每个事物的定义时,__THROW导致了这样的情况:
# if !defined __cplusplus && __GNUC_PREREQ (3, 3)
// stuff that doesn't happen
# else
# if defined __cplusplus && __GNUC_PREREQ (2,8)
# define __THROW throw ()
# define __THROWNL throw ()
# define __NTH(fct) __LEAF_ATTR fct throw ()
# define __NTHNL(fct) fct throw ()
// continuation to the …Run Code Online (Sandbox Code Playgroud) 我的理解是,在现代 C++ 中,最好使用throw而不是返回错误代码。也就是说,如果我有一些功能:
void MyFunction(int foo, double bar){
// Do some stuff....
if (exception_criteria_met){
throw "Call to MyFunction with inputs" << foo << " and " << bar << " failed because ...";
}
};
Run Code Online (Sandbox Code Playgroud)
这是否意味着,为了正确处理这个问题,那么在所有调用 的函数中MyFunction,我必须实现如下所示的内容:
void MyOuterFunction(int foo){
// Do some stuff...
try {
MyFunction(foo, bar);
} catch (const char * exception_message) {
throw "Call to MyOuterFunction with inputs " << foo << " failed because call to MyFunction with ....";
};
Run Code Online (Sandbox Code Playgroud)
那么这是否意味着我必须对调用该函数调用堆栈的所有函数一直执行相同类型的错误处理? …
有没有一种方法可以在不使用Java中的if语句的情况下在函数中引发异常?例如,如果一个整数小于0,我想抛出一个异常,但是当检查该特定条件时,我不想使用if(或switch)语句。
好的,所以我是错误处理的新手,我已经看到了一些例子,但我还没有看到这个问题的答案.我将使用一些真正的基本示例代码来显示我在问什么.
if(some condition){
throw Exception()
}
//Some random code in between
echo "Code between throw() and Catch()";
catch(Exception $e){
//handle the caught exception
}
Run Code Online (Sandbox Code Playgroud)
所以基本上,我的问题是 - 如果if()中的条件导致抛出异常,随机echo语句会执行,还是会跳过并直接进入异常的catch()?