473,396 Members | 1,900 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,396 software developers and data experts.

question on try/catch

Hi,

I tried the following code about try/catch (from cplusplus.com)

// exceptions
#include <ciostream>
using namespace std;

int main () {
char myarray[10];
try
{
for (int n=0; n<=10; n++)
{
if (n>9) throw "Out of range";
myarray[n]='z';
}
}
catch (char * str)
{
cout << "Exception: " << str << endl;
}
return 0;
}

It's expected to give "Exception: Out of range".
but i complied in g++ 3.3.3. and run it, the output is "Aborted".

Anyone help me to explain what's wrong here? or normal?

Thanks a lot!

xuatla
Jul 23 '05 #1
4 2993
xuatla wrote:
Hi,

I tried the following code about try/catch (from cplusplus.com)

// exceptions
#include <ciostream>
using namespace std;

int main () {
char myarray[10];
try
{
for (int n=0; n<=10; n++)
{
if (n>9) throw "Out of range";
myarray[n]='z';
}
}
catch (char * str)
{
cout << "Exception: " << str << endl;
}
return 0;
}

It's expected to give "Exception: Out of range".
but i complied in g++ 3.3.3. and run it, the output is "Aborted".

Anyone help me to explain what's wrong here? or normal?


Try making the catch:

catch( const char* str )

I don't use g++ much, maybe it considers literal strings such as "Out of
range" to be const char*. Therefore it wouldn't match/catch a
non-const...

Jul 23 '05 #2

"xuatla" <xu****@gmail.com> ??????:d0**********@daisy.noc.ucla.edu...
Hi,

I tried the following code about try/catch (from cplusplus.com)

// exceptions
#include <ciostream>
using namespace std;

int main () {
char myarray[10];
try
{
for (int n=0; n<=10; n++)
{
if (n>9) throw "Out of range";
myarray[n]='z';
}
}
catch (char * str)
should be:
catch (char const *str)
{
cout << "Exception: " << str << endl;
}
return 0;
}

It's expected to give "Exception: Out of range".
but i complied in g++ 3.3.3. and run it, the output is "Aborted".

Anyone help me to explain what's wrong here? or normal?

Thanks a lot!

xuatla

Jul 23 '05 #3
xuatla wrote:
Hi,

I tried the following code about try/catch (from cplusplus.com)

// exceptions
#include <ciostream>
using namespace std;

int main () {
char myarray[10];
try
{
for (int n=0; n<=10; n++)
{
if (n>9) throw "Out of range";
myarray[n]='z';
}
}
catch (char * str)
{
cout << "Exception: " << str << endl;
}
return 0;
}

It's expected to give "Exception: Out of range".
but i complied in g++ 3.3.3. and run it, the output is "Aborted".

Anyone help me to explain what's wrong here? or normal?


You're not catching the exception you threw. "Out of range" is of type
const char[13], which gets converted to const char* before being thrown. But
you're not catching const char*, and therefore the exception gets out. The
result is correct (std::abort() being called).

Jul 23 '05 #4
"xuatla" <xu****@gmail.com> wrote in message
news:d0**********@daisy.noc.ucla.edu...
#include <ciostream>
using namespace std;

int main () {
char myarray[10];
try
{
for (int n=0; n<=10; n++)
{
if (n>9) throw "Out of range";
myarray[n]='z';
}
}
catch (char * str)
{
cout << "Exception: " << str << endl;
}
return 0;
}

It's expected to give "Exception: Out of range".
but i complied in g++ 3.3.3. and run it, the output is "Aborted".

Anyone help me to explain what's wrong here? or normal?


What happens if you change

catch (char * str)

to

catch (const char * str)

?
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Mark | last post by:
Apologies for the lengthy posting.... In a previous posting:...
5
by: Kevin Jackson | last post by:
In the following code snippet, will the finally block be executed when the throw is executed in the catch block???? I'm assuming it will. catch (Exception e) { // if...
1
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from...
2
by: ETL | last post by:
Hi, I think this question has an easy answer but I can't seem to find anything online that lists best practices for my problem. I have a C# app that uses an exception handling tree similar to...
13
by: Woody Splawn | last post by:
I have a try catch statement in a fucntion that is supposed to return a true or a false My code looks like this: Try mySqlConnection.Open() Dim Da1 As New SqlDataAdapter("Select JnlType,...
7
by: Tiraman | last post by:
Hi , I am using allot the try catch in my code and the question is if it is good ? it will decrease my performance ? one more question
5
by: Patrick Dickey | last post by:
Hello, All! I'm modifying some source code that I downloaded from Planet-source-code a while back, and have a question about Try Catch statements. Basically, I'm wondering if I can do a Try with...
10
by: tony | last post by:
Hello!! As you know every user defined exception must be derived from class Exception. Now to my question if I write catch then every exception will be caught. If I instead write...
20
by: fniles | last post by:
I am using VS2003 and connecting to MS Access database. When using a connection pooling (every time I open the OLEDBCONNECTION I use the exact matching connection string), 1. how can I know how...
5
by: Vijay | last post by:
Hi All, I am not able to figure out what exactly happening in below code. what is control flow. Can anyone clear my confusion? Code: class A { public: A(){cout<<"In Constructor\n";}
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.