Following fails to catch a exception
int *i; //intentionally uninitialized
try {
*i = 6;
}
catch (const runtime_error e) {
cout << "caught!" << endl;
}
is it actually catching a runtime error or an exception?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The line
*i = 6;does not throw aruntime_error; it results in undefined behavior.The uninitialized pointer will be dereferenced and your program will try to write the value six to wherever it points (if it points anywhere). This results in undefined behavior. In most cases, this means your program will either crash immediately or will crash later on because you will have corrupted something important in memory.
There is no way to “catch” this sort of error as an exception in standard C++; you need to write your code such that you don’t do things like this.