H
Henryk
I did a lot of delphi GUI programming recently.
In my experience most of the time you just want to throw a standard
exception with a descriptive message. Then all calling functions can
handle this exception as they like and finally show a message to the
user or not. Only in a few occasions I need some special exception
types to behave differently depending on the error.
So what you basically do is
// delphi syntax but the meaning should be clear
try
if ({some error check}) then
// what a nice one-liner...
raise Exception.CreateFmt('Error parsing line %d: Command unknown',
nLine);
except
on E: Exception do ... show user E.Message in a message box
end;
I tried to achieve something similar in c++
try {
if (/*some error check*/) {
// 3 lines needed and also not very readable...
std:strstream msg;
msg << "Error parsing line " << nLine << ": Command unknown" <<
std::ends;
throw std::exception(msg.str());
}
}
catch(std:exception e) {
show user e.what() in a message box
}
I could write my own derived exception class that provides a
sprintf-like function, but I was wondering if standard c++ provides
something similar (and I couldn't find it).
Btw, I like the printf formatting style over the stream style. The
first one is better readable and it's much easier to implement mutiple
languages.
How do you do this kind of exception handling in your programs?
Greets
Henryk
In my experience most of the time you just want to throw a standard
exception with a descriptive message. Then all calling functions can
handle this exception as they like and finally show a message to the
user or not. Only in a few occasions I need some special exception
types to behave differently depending on the error.
So what you basically do is
// delphi syntax but the meaning should be clear
try
if ({some error check}) then
// what a nice one-liner...
raise Exception.CreateFmt('Error parsing line %d: Command unknown',
nLine);
except
on E: Exception do ... show user E.Message in a message box
end;
I tried to achieve something similar in c++
try {
if (/*some error check*/) {
// 3 lines needed and also not very readable...
std:strstream msg;
msg << "Error parsing line " << nLine << ": Command unknown" <<
std::ends;
throw std::exception(msg.str());
}
}
catch(std:exception e) {
show user e.what() in a message box
}
I could write my own derived exception class that provides a
sprintf-like function, but I was wondering if standard c++ provides
something similar (and I couldn't find it).
Btw, I like the printf formatting style over the stream style. The
first one is better readable and it's much easier to implement mutiple
languages.
How do you do this kind of exception handling in your programs?
Greets
Henryk