www.digitalmars.com         C & C++   DMDScript  

c++ - Some Problem with exception

reply "Neo" <newneo2 yahoo.com> writes:
hi
i tried simple code

char *c= NULL;
try{
    *c= 10;
}
catch(...){
    coutt<<"NULL POINTER ACCESS";
}

but this doesn't work as expected. same code works in VC++. i have compiled
it with -Ae option.
Oct 13 2002
next sibling parent "Walter" <walter digitalmars.com> writes:
What happens when you run it?

"Neo" <newneo2 yahoo.com> wrote in message
news:aoekhu$di0$1 digitaldaemon.com...
 hi
 i tried simple code

 char *c= NULL;
 try{
     *c= 10;
 }
 catch(...){
     coutt<<"NULL POINTER ACCESS";
 }

 but this doesn't work as expected. same code works in VC++. i have
compiled
 it with -Ae option.
Oct 14 2002
prev sibling next sibling parent Jan Knepper <jan smartsoft.us> writes:
I think VC might use try and catch for language exceptions as well as regular
(hardware) exceptions.
Basically there is nothing wrong with the code, except when try/catch would
handle the hardware exceptions too. Than *c = 10 would throw an exception.



Neo wrote:

 hi
 i tried simple code

 char *c= NULL;
 try{
     *c= 10;
 }
 catch(...){
     coutt<<"NULL POINTER ACCESS";
 }

 but this doesn't work as expected. same code works in VC++. i have compiled
 it with -Ae option.
Oct 14 2002
prev sibling parent Larry Brasfield <larry_brasfield snotmail.com> writes:
In article <aoekhu$di0$1 digitaldaemon.com>, 
Neo (newneo2 yahoo.com) says...
 hi
 i tried simple code
 
 char *c= NULL;
 try{
     *c= 10;
 }
 catch(...){
     coutt<<"NULL POINTER ACCESS";
 }
 
 but this doesn't work as expected.
According to the C++ standard, (and assuming that 'NULL' expands to '0'), that behavior of that code is undefined. That means there is no correct expectation other than "anything can happen".
 same code works in VC++.
The fact that your unduly narrow expectation was met in no way implies a fault in DMC++. Since any behavior is allowed, there cannot be any incorrect result. -- -Larry Brasfield (address munged, s/sn/h/ to reply)
Oct 14 2002