Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ Exceptions

Reply
Thread Tools

C++ Exceptions

 
 
praviarun@gmail.com
Guest
Posts: n/a
 
      01-11-2007
I found that the following code catches exception on gcc 4.0
i.e it prints "caught standard exception"


try{
vector<int> test;
test.at(0); // this line throws an exception

}
catch(...)
{
printf("caught standard exception");

}

but the following code which dereferences a null pointer does not throw
an exception

try{
int *p = 0;
*p =10;

}
catch(...)
{

printf("caught dereferencing null pointer");

}

This code simply terminates without catching an exception
I am using Xcode

Please let me know how i could catch this exception


Praveen

 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      01-11-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> Please let me know how i could catch this exception
>


In standard C++, if you don't throw it, you can't catch it. Hardware
"exceptions" are not the same thing as C++ "exceptions".

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
Reply With Quote
 
 
 
 
Dizzy
Guest
Posts: n/a
 
      01-11-2007
(E-Mail Removed) wrote:

> but the following code which dereferences a null pointer does not throw
> an exception
>
> try{
> int *p = 0;
> *p =10;
>
> }
> catch(...)
> {
>
> printf("caught dereferencing null pointer");
>
> }


Of course it does not.

>
> This code simply terminates without catching an exception
> I am using Xcode


Where does in the C++ standard text says that dereferencinging an invalid
pointer (null included) should throw a C++ exception ?

> Please let me know how i could catch this exception


There is no C++ exception thrown, thus there is no C++ exception to catch.
If you do not want to have such an issue make sure you do not dereference
invalid pointers (using references helps with that by requiring to be
initilized although of course you may still have object lifetime issues).

You seem to confuse "exceptions" as a general term/concept with C++
exceptions language feature. They are not the same. What you do there is
just to invoke "undefined behaivour". Your thread is very similar to one
discussed here recently about exceptions (not) thrown on division by 0.

--
Dizzy
http://dizzy.roedu.net

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      01-11-2007
Pete Becker wrote:
> (E-Mail Removed) wrote:
> >
> > Please let me know how i could catch this exception
> >

>
> In standard C++, if you don't throw it, you can't catch it. Hardware
> "exceptions" are not the same thing as C++ "exceptions".


Right. In non-standard C++, exceptions of a sort can sometimes thrown
for errors of this kind (cf. Microsoft's "structured exceptions"), but
for more on that, the OP will need to ask on a forum that deals with
his specific compiler or platform.

Cheers! --M

 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-14-2007
(E-Mail Removed) wrote:

> This code simply terminates without catching an exception
>
> Please let me know how i could catch this exception


Can not, but you can try use signal handlers (see signal.h), if your
system generates and allows to catch signals in the case. I am not
shure you will can continue execution after the error, but will can
print error message.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exceptions - How do you make it work like built-in exceptions? Lie Python 3 01-14-2008 06:45 PM
Exceptions + Performance on path without exceptions gratch06@gmail.com C++ 3 04-16-2007 08:52 PM
Checked exceptions vs unchecked exceptions Ahmed Moustafa Java 5 07-14-2004 01:46 PM
ImageList Errors - Exceptions and...need HELP Herr Lucifer ASP .Net 0 06-21-2004 10:21 AM
Custom exceptions -- inherit from exceptions.Exception? Paul Miller Python 3 11-12-2003 09:24 AM



Advertisments