473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 2905
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Páll Ólafsson | last post by:
Hi I have a problem with the Microsoft.ApplicationBlocks.ExceptionManagement? I can't get it to work in RELEASE mode? If I run the project in debug mode the block works fine but when I run the...
6
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
6
by: Kevin Jackson | last post by:
Let's say we log exceptions to the windows application event log using the Exception Management Application Block. Is there a pattern that can be used so the exception is logged only once and not...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
6
by: Nick Reeves | last post by:
While developing the below code I noticed that exceptions were not working as expected. An exception is thrown inside an XML import DLL and not being caught inside the Main() code fragment,...
4
by: Ele | last post by:
When Exception handling disabled compiler still spits out "C++ exception handler used." Why is that? Why does it ask for "Specify /EHsc"? Thanks! c:\Program Files\Microsoft Visual Studio...
11
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
41
by: Stuart Golodetz | last post by:
Hi all, Just wondering whether there's any reason why exception specifications are enforced at runtime, rather than at compile-time like in Java? (This was prompted by reading an article on...
4
by: DHS1 | last post by:
Hey guys. I have a lab that is due in two weeks, but I wanted to start on it now. Problem is, I'm at home during christmas break so I can't ask my professors. Here's my problem: I am given a very...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.