Try-Catch Problem in c++ - c++

I am trying queue implementation in c++. During that I am having this problem.
void Queue::view()
{
int i;
try
{
if(Qstatus==EMPTY)
{
UnderFlowException ex = UnderFlowException("\nQUEUE IS EMPTY");
throw ex;
}
}
i=front;
cout<<"Queue contains...\n";
while(i <= rear)
{
cout<<queue[i]<<" ";
i++;
}
}
This gives an error as :
error: expected ‘catch’ before ‘i’
I think this problem is arises since I doesn't written catch block below try block.
But If want to write the catch block in main(), ( like in this case ), how could I do that?
Before, that Could I do that? If not Why?

catch block must follow the try block. If you want the catch to be in main - that's where the try has to be too. You can throw everywhere, doesn't have to be inside a try block within the same function.
It should be something like this:
void Queue::view()
{
int i;
if(Qstatus==EMPTY)
{
UnderFlowException ex = UnderFlowException("\nQUEUE IS EMPTY");
throw ex;
}
i=front;
cout<<"Queue contains...\n";
while(i <= rear)
cout<<queue[i]<<" ";
}
/// ...
int main()
{
Queue q;
try{
q.view();
}
catch(UnderFlowException ex)
{
/// handle
}
catch (...)
{
/// unexpected exceptions
}
// follow the success/handled errors
}

You simply need to remove the try block. A try block always goes with a catch.
void Queue::view()
{
int i;
if(Qstatus==EMPTY)
{
ex = UnderFlowException("\nQUEUE IS EMPTY");
throw ex;
}
i=front;
cout<<"Queue contains...\n";
while(i <= rear)
cout<<queue[i]<<" ";
}
You can then include a try/catch construct in your main.
int main()
{
Queue queue;
try
{
queue.View()
}
catch(UnderFlowException ex)
{
//handle ex
}
return 0;
}

All try blocks need at least one associated catch block. You should remove the try block if you have no intentions of handling any exceptions here. Exceptions can be (and usually should be!) thrown outside of a try block.

Make your code catch and rethrow the exception, like this:
try
{
if(Qstatus==EMPTY)
{
UnderFlowException ex = UnderFlowException("\nQUEUE IS EMPTY");
throw ex;
}
} catch( ... ) {
throw; // rethrow whatever exception we just catched
}
Although you don't even need the try block in the first place. Looks like just throw ex; would work, since you don't intend to catch it but just throw it.

try{
}
catch(Exception ex){
}
Catch must be immediately after try. These are the rules.

Related

user defined exception handling

This is a c++ program to calculate average, and grade using 5 marks.
If marks entered are greater than 100 or less than 0, student exception should be thrown.
#include<iostream>
#include<exception>
using namespace std;
class lessex:public exception
{
public:
void what()
{
cout<<"Mark less than 0"<<endl;
}
};
class morex:public exception
{
public:
void what()
{
cout<<"Mark greater than 100"<<endl;
}
};
class student
{
string name;
string rollno;
int marks[5];
double avg;
char g;
public:
void get();
void aveg();
void grade();
void print();
};
void student::get()
{
cin>>name;
cin>>rollno;
for(int i=0;i<5;i++)
{
try{
cin>>marks[i];
if(marks[i]>100)
{
morex d;
throw d;
}
}
catch(morex &e)
{
/*e.what();*/
throw ;
}
try{
if(marks[i]<0)
{
lessex d;
throw d;
}
}
catch(lessex &e)
{
/*e.what();*/
throw ;
}
}
}
void student::aveg()
{
int sum=0;
for(int i=0;i<5;i++)
{
sum=sum+marks[i];
}
avg=sum/5;
}
void student::grade()
{
if(avg>90)
g='S';
else
g='Z';
}
void student::print()
{
cout<<name<<endl;
cout<<rollno<<endl;
cout<<g<<endl;
}
int main()
{
student s;morex e;lessex e1;
try{
s.get();
}
catch(morex &e)
{
e.what();
}
catch(lessex &e1)
{
e1.what();
}
s.aveg();
s.grade();
s.print();
return 0;
}
However, my program does not successfully exit after encountering exception in the main function.
Why is it continuing with s.aveg,grade,etc.
Why is my program not exiting after encountering exception-from main function? Why is it continuing with s.aveg,grade,etc.
You catch the exception, and then leave the catch block. Execution continues normally after that. Exceptions aren't re-thrown automatically at the end of a handler's block. That would maddening, what's the point of catching if you can't handle the error and continue running?
If you want the exception re-thrown, you need to add an explicit throw; in the handler. Like you already do in student::get(). Or just not have a try-catch block there. The program will terminate without "s.aveg,grade,etc." being executed.
Or, assuming you intent is not terminate, but to exit gracefully without executing other functions, you can do as user4581301 suggested. Move those function calls into the try block. That way, if an exception is thrown before their execution, they will not run before or after the handler.
You continue execution, without exiting, after catching the exception, and that's why the program isn't exiting.
First, you should follow the convention that what returns a string and doesn't print anything:
class lessex : public exception
{
public:
const char* what() const noexcept override
{
return "Mark less than 0";
}
};
class morex : public exception
{
public:
const char* what() const noexcept override
{
return "Mark greater than 100";
}
};
Then, you're overcomplicating things rather a lot in get;
void student::get()
{
cin >> name;
cin >> rollno;
for(int i = 0; i < 5; i++)
{
cin >> marks[i];
if (marks[i]>100)
{
throw morex();
}
if(marks[i]<0)
{
throw lessex();
}
}
}
and exceptions shouldn't be used like error codes and caught after each potentially throwing call, you normally write the "happy path" (the assumed-to-be-error-free path) and handle exceptions outside it:
int main()
{
try
{
// This part handles the normal case, assuming that all goes well.
student.s;
s.get();
s.aveg();
s.grade();
s.print();
}
// This part handles the exceptional case when something goes wrong.
catch (std::exception& ex)
{
std::cerr << ex.what();
}
}
(Your design is somewhat questionable since throwing from get can leave the object in an invalid state. You might want to rethink it.)
It is continuing because after you catch the exception you don't do anything about it. You have to specify what you would do inside the catch block when the specific exception is thrown.
Alternatively you could also move the other functions like s.aveg(); s.grade(); s.print(); inside try{
s.get();
}
This will prevent the aveg, grade and print functions from stop executing once an exeption is hit

How to assert that an error message equals to certain char expression

I am trying to call a function from my Calculator class in a test class.
How do I assert that exception message thrown by Calculator equals to one I expect?
My calculator code:
void Calculator::add() {
if (_stack.empty()) {
throw std::runtime_error("empty stack");
}
else if (_stack.size() == 1) {
throw std::runtime_error("Wrong Value");
}
else if (_stack.size() > 2) {
throw std::runtime_error("Wrong Value");
}
else {
double res = this->pop() + this->pop();
this->push(res);
}
}
Here is my test class:
TEST_METHOD(should_throw_error_if_stack_is_empty) {
Assert::AreEqual([] {
Calculator* myCalculator = new Calculator();
try {
myCalculator->add();
}
catch (std::runtime_error const& ex)
{
return (ex.what());
}
}, "empty stack");
}
The reason it does not work: Value returned by lambda function has type const char* while my expression type is const char. How should I do it?
And is the approach I follow a good practice in general? I mean catching the exceptions thrown by class in a unit test?

C++ successful `try` branch

Let's consider some artificial C++ code:
int i = 0;
try { someAction(); }
catch(SomeException &e) { i = -1; }
i = 1;
... // code that uses i
I want this code to assign -1 to i in case of someAction() throws exception and assign 1 in case if there was no exception. As you can see now this code is wrong because i finally always becomes 1. Sure, we can make some trick workarounds like:
int i = 0;
bool throwed = false;
try { someAction(); }
catch(SomeException &e) { throwed = true; }
i = throwed ? -1 : 1;
... // code that uses i
My question is: is there anything in C++ like "successful try branch", where I do some actions in case if there were no any throws in try block?
Something like:
int i = 0;
try { someAction(); }
catch(SomeException &e) { i = -1; }
nocatch { i = 1; }
... // code that uses i
Surely, there is no nocatch in C++ but maybe there is some common beautiful workaround?
int i = 0;
try { someAction(); i = 1; }
catch(SomeException &e) { i = -1; }
Aside from the simple solution
try
{
someAction();
i = 1;
}
catch(SomeException &e)
{
i = -1;
}
you should consider what you are planning to do with the value of i further in the code - use it in if statements? That is poor design, you could simply put all the code inside the braces after try and catch respectively.

With Do Begin Statement in C++ Builder

I wanna know how to write the following codes in C++ Builder by using With Do Begin statement similar to Delphi.
I tried with ComboBox->Text .... do ... try and it's not working. I tried with just do ComboBox->Text .... try, also not working.
if (ComboBox->Text.operator==(String("C++ Builder XE7")))
{
try
{
// do something
if ((Form1->Memo1->Lines->Text).Pos("<") !=0)
{
// do something
}
}
catch(Exception &ex)
{
ShowMessage(ex.ToString());
}
if (ComboBox->Text.operator==(String("C++ Builder XE8")))
{
try
{
// do something
if ((Form1->Memo1->Lines->Text).Pos("<") !=0)
{
// do something
}
}
catch(Exception &ex)
{
ShowMessage(ex.ToString());
}
There is no equivalent to Delphi's with statement in C++. The best you can do in C++ is use pointers/references instead, eg:
TComboBox *cb = ComboBox;
TStrings *lines = Form1->Memo1->Lines;
if (cb->Text == "C++ Builder XE7")
{
try
{
// do something
if (lines->Text.Pos("<") != 0)
{
// do something
}
}
catch(const Exception &ex)
{
ShowMessage(const_cast<Exception&>(ex).ToString());
}
}
if (cb->Text == "C++ Builder XE8")
{
try
{
// do something
if (lines->Text.Pos("<") != 0)
{
// do something
}
}
catch(const Exception &ex)
{
ShowMessage(const_cast<Exception&>(ex).ToString());
}
}

Error checking on many function calls

Sometimes when I am programming in C++/C I end up calling the same function multiple times and I was wondering what is the most efficient way to check for errors for all of those calls? Using if else statements take up a lot of code and look ugly. I have come up with my own way of checking for errors, perhaps there is a better way that I should use.
int errs[5] = {0};
errs[0] = functiona(...);
errs[1] = functiona(...);
...
errs[5] = functiona(...);
for (int i = 0; i < 5; i++)
{
if (err[i] == 0)
MAYDAY!_wehaveanerror();
}
Note: I understand that using try and catch might be better for C++ as it would solve this problem by throwing an exception on the first error, but the problem with that is that it is not compatible with a lot of functions that return error codes such as the Windows API. Thanks!
You could write some pseudo-C++ like this:
struct my_exception : public std::exception {
my_exception(int); /* ... */ };
int main()
{
try
{
int e;
if ((e = function()) != SUCCESS) { throw my_exception(e); }
if ((e = function()) != SUCCESS) { throw my_exception(e); }
if ((e = function()) != SUCCESS) { throw my_exception(e); }
}
catch (my_exception & e)
{
std::cerr << "Something went wrong: " << e.what() << "\n";
}
}
If...IF the function has a chance to throw a different error you should also add a catch all.
struct my_exception : public std::exception {
my_exception(int); /* ... */ };
int main()
{
try
{
int e;
if ((e = function()) != SUCCESS) { throw my_exception(e); }
if ((e = function()) != SUCCESS) { throw my_exception(e); }
if ((e = function()) != SUCCESS) { throw my_exception(e); }
}
catch (my_exception & e)
{
std::cerr << "Something went wrong: " << e.what() << "\n";
}
catch (...)
{
//Error Checking
}
}
What about handling the checking in a function?
void my_function() {
if (!create_window())
throw Error("Failed to create window");
}
int main() {
try {
my_function();
} catch (const Error& e) {
cout << e.msg << endl;
} catch (...) {
cout << "Unknown exception caught\n"
}
return 0;
}
If you're calling the same function over and over again, the most succinct way might be to use a macro. I would suggest something like:
#define CHECKERROR(x) if(x == 0) wehaveanerror()
CHECKERROR(function(...));
CHECKERROR(function(...));
Obviously, this macro would be very specific to the particular function and error handler involved, so it may be prudent to undef it after those calls.
Doing it more old-school, but keeping w/ the original error response but responding as soon as an error occurs w/o looking ugly:
#define callcheck(r) if ((r)==0) MAYDAY!_wehaveanerror()
callcheck(functiona(...));
callcheck(functiona(...));
...