468,119 Members | 2,108 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,119 developers. It's quick & easy.

exception handling problem

Hi

I have function:

control_input(string input){
if(input!="open"){
throw "Invalid command";
}
}

and i call it in main:

int main(){
string input;

cin<<input;

try{
control_input(input);
}
catch(strin e){
cout<<e<<endl;
}

return 0;
}

For some reason that does not work. If it is time to catch exception, my
program ends with that kind of message:

terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at
Aborted

What is wrong and how to make it work correctly?

Juhan.
Aug 2 '05 #1
4 2652
use stdexcept:

#include <stdexcept>
throw std::invalid_argument("Invalid command");

and

...
}catch(const std::exception& e){
cout << e.what() << endl;
}

Aug 2 '05 #2
Juhan Voolaid sade

control_input(string input){
if(input!="open"){
throw "Invalid command";
}
}

and i call it in main:

int main(){
string input;

cin<<input;

try{
control_input(input);
}
catch(strin e){
cout<<e<<endl;
}

return 0;
}


I would argue that this is not the same code as the one you're
compiling, more likely a retype with typos, which will not compile.

Tobias

--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Aug 2 '05 #3
Juhan Voolaid <ju*@hot.ee> wrote in news:42********@news.infonet.ee:
Hi

I have function:

control_input(string input){
if(input!="open"){
throw "Invalid command";
}
}

and i call it in main:

int main(){
string input;

cin<<input;

try{
control_input(input);
}
catch(strin e){
cout<<e<<endl;
}

return 0;
}

For some reason that does not work. If it is time to catch exception, my program ends with that kind of message:

terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at
Aborted

What is wrong and how to make it work correctly?


You're not throwing a string (or even a std::string), you're throwing a
const char*

Oh, and there are no default return types in c++, control_input must
declare what type it returns.
Aug 2 '05 #4
> You're not throwing a string (or even a std::string), you're throwing a
const char*


which means you have to throw like this:

throw string("ERROR: UFO found...");
Aug 3 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Páll Ólafsson | last post: by
6 posts views Thread by Daniel Wilson | last post: by
6 posts views Thread by Kevin Jackson | last post: by
44 posts views Thread by craig | last post: by
6 posts views Thread by Nick Reeves | last post: by
41 posts views Thread by Stuart Golodetz | last post: by
4 posts views Thread by DHS1 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.