|
One more detail - on Windows, you must open the file in "binary" mode using the O_BINARY flag (for open) or "rb" (for fopen).
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
I am trying to catch a divide-by-zero error with the following code:
#include <iostream>
using namespace std;
int main() {
try {
int a = 0;
int x = 5 / a;
}
catch (exception e)
{
cout << e.what() << endl;
}
catch (...)
{
cout << "caught ..." << endl;
}
}
For some reason, neither of my catch blocks gets executed. Anyone know how to catch the exception?
Thank you.
|
|
|
|
|
AFAIK, there's no way to do it.
You'll have to catch it manually and throw.
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
I've not used it, but maybe take a look at Handling the Divide by Zero Exception in C++[^], similar to what Maximilien said.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
modified 5-Jan-24 13:08pm.
|
|
|
|
|
The article mentioned in the previous post does it by doing divisions in a function that checks for a zero divisor and throws an exception if it detects one. If you don't want to use such a function, it gets rather complicated.
If you're running on Windows, you need to handle the structured exception STATUS_FLOAT_DIVIDE_BY_ZERO or STATUS_INT_DIVIDE_BY_ZERO . If you're running on Linux, you need to handle the POSIX signal SIGFPE . You can also handle the use of a bad pointer with these techniques. The handler that receives the structured exception or POSIX signal can throw a C++ exception, which you can then catch in the usual way.
The following article goes into the details. But because it also describes a thread framework, you'll have to sift through it to extract the specific code that you need. In particular, see the sections "Receiving a Windows Structured Exception" and "Receiving a POSIX Signal".
Robust C++: Safety Net[^]
|
|
|
|
|
To me that was always the 'fun' part of C++ versus Java and C# - figuring out exactly what was being thrown in the first place.
|
|
|
|
|
In Windows, there are additional, O/S-level exceptions, that provide this sort of information. Look for "Structured Exception Handling". If you use MSVC, you may use the _set_se_handler function in main() and in the main function of each thread to convert Structured Exceptions to C++ exceptions.
There may be similar O/S- and compiler-specific methods in other environments.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
I have the following file code:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
string path = "student/text/readpractice.txt";
try {
ifstream file;
file.open(path); if (!file) {
throw runtime_error("File failed to open.");
}
cout << file.rdbuf(); file.close();
}
catch (exception& e) {
cerr << e.what() << endl;
}
return 0;
}
Can someone help me understand how this line works:
if (!file) {
How can you use the exclamation point operator on what is probably a class?
Is there some type of overloading that has occurred? I looked thru the code, and I couldn't find the ! point being overloaded.
Any ideas?
Thanks.
|
|
|
|
|
|
Interesting.
I tried doing a "Step Into" on that line in the debugger, but it won't go to it.
If I do a "Go to Definition", it goes to this in xiosbase:
_NODISCARD bool __CLR_OR_THIS_CALL operator!() const noexcept {
return fail();
}
However, I put a breakpoint there, and it doesn't break.
|
|
|
|
|
I wrote the following code to output the Greek word "Δέλτα":
#include <iostream>
#include <io.h>
#include <fcntl.h>
using namespace std;
int main() {
_setmode(_fileno(stdout), _O_U16TEXT);
cout << "\u0394\u03AD\u03BB\u03C4\u03B1" << endl;
return 0;
}
For some reason, I get this error:
---------------------------
Microsoft Visual C++ Runtime Library
---------------------------
Debug Assertion Failed!
Program: ...sers\Owner\source\repos\assert_test\x64\Debug\assert_test.exe
File: minkernel\crts\ucrt\src\appcrt\lowio\write.cpp
Line: 659
Expression: buffer_size % 2 == 0
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
---------------------------
Abort Retry Ignore
---------------------------
Any ideas why the code is producing that error?
Thank you.
|
|
|
|
|
First things first, you're ignoring the return value of _setmode. If it's -1, the call failed, but you'll never know.
Second, after reading the documentation on _setmode and associated error links, it appears you cannot use cout with the mode of stdout switched to Unicode. You have to use wprintf instead:
#include <iostream>
#include <io.h>
#include <fcntl.h>
using namespace std;
int main()
{
int r;
r =_setmode(_fileno(stdout), _O_U16TEXT);
if (r == -1)
perror("Cannot set mode");
wprintf(L"\u0394\u03AD\u03BB\u03C4\u03B1\n");
return 0;
}
CORRECTION: You CAN use COUT, but you must use the wide version of it, but note the L in front of the string being output. It MUST be there:
#include <iostream>
#include <io.h>
#include <fcntl.h>
using namespace std;
int main()
{
int r;
r =_setmode(_fileno(stdout), _O_U16TEXT);
if (r == -1)
perror("Cannot set mode");
wcout << L"\u0394\u03AD\u03BB\u03C4\u03B1" << endl;
return 0;
}
|
|
|
|
|
I wrote this code:
class ChessPiece
{
};
int main()
{
cout << "size:" << sizeof(ChessPiece) << endl;
}
For some reason, it prints out 1 instead of 0. Anyone know why?
Thanks.
|
|
|
|
|
Probably because there has to be some minimum size of a class. You could study the C++ standard to see why.
|
|
|
|
|
Perhaps take a look at this FAQ.
Stroustrup: C++ Style and Technique FAQ[^]
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
That is interesting.
That page references a 'better' link. Same answer is there.
Standard C++[^]
Myself though if Stroustrop is the name then I would consider that better.
modified 2-Jan-24 12:43pm.
|
|
|
|
|
jschell wrote: if Stroustrop is the name then I would consider that better. Agreed.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Voluntarily removed - incorrect forum.
modified 1-Jan-24 23:53pm.
|
|
|
|
|
The << in this is not shift left, it is operator<<() operating on whatever QStringList() returns. It might be concatenating them, or appending them to an array, or whatever.
It can handle both char* and QString because it probably has overloads for both types, or possibly because QString has an operator for returning a char* .
I'm guessing this from the context because the C++ standard library also uses << this way - you would have to consult the QT documentation to be sure.
|
|
|
|
|
Thank you.
Appreciate your help.
Am I understanding it correctly - "<<" is pretty much like passing a parameter to a function ?
I am still not clear about "where -c " is passed to - is it an option to QStringList or "/bin/sh"?
I believe it is "passed to QStringList" since that is a second parameter to QProcess.
Perhaps now I can make more sense from QT docs, both - QProcess and QStringList.
I also need to look at the source of "sh"...
|
|
|
|
|
I'm in agreement with Richard on what is actually happening there. It looks like the QStringList is being used to join all the arguments together to make a complete command line for passing to the QProcess .
/bin/sh is the Bourne shell, you should be able to find the manual page for it online or by typing "man sh" in your Linux terminal. I don't know why it is using a shell instead of executing the command directly, but I suspect there is a good reason. Maybe for wildcard parsing or terminal I/O buffering.
|
|
|
|
|
Message Closed
modified 2-Jan-24 12:45pm.
|
|
|
|
|
xterm will only return whether xterm itself failed or not. So for example xterm -e /bin/false returns true (0), since xterm executed successfully.
I'm suspicious about your approach, here though. Can you not run your command directly via QProcess, rather than launching an xterm? I am assuming 2 things here: 1) you need to capture the output of command , at least in part, and 2) QProcess can do that for you, similar to using pipe() , rather than using system() to run a shell command from a program.
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
k5054 wrote: I'm suspicious about your approach
I agree with that.
|
|
|
|
|
Message Closed
modified 2-Jan-24 12:45pm.
|
|
|
|