473,404 Members | 2,114 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,404 software developers and data experts.

Which exception it is?

mos
Hi!
When call a null function pointer will cause a execption, but it can't
be caught as std::exception, then which exception it is?
The following code is a example:

typedef void (*testfunc)(int a);

void test()
{
testfunc func = NULL;
func(10);
iTest* p = NULL;
p->Test(10);
}

int main()
{
try
{
test();
}
catch(std::exception& x)
{
std::cout << "exception: " << x.what() << std::endl;
}
catch(...)
{
std::cout << "unknown" << std::endl;
}
return 0;
}
Jun 23 '07 #1
9 1828
On 23 Jun, 09:46, "mos" <mmosqu...@163.comwrote:
Hi!
When call a null function pointer will cause a execption,
Says who?
but it can't
be caught as std::exception, then which exception it is?
The following code is a example:

typedef void (*testfunc)(int a);

void test()
{
testfunc func = NULL;
func(10);
Dereferencing a null pointer.
iTest* p = NULL;
p->Test(10);
Dereferencing a null pointer
>
}

int main()
{
try
{
test();
}
catch(std::exception& x)
{
std::cout << "exception: " << x.what() << std::endl;
}
catch(...)
{
std::cout << "unknown" << std::endl;
}
return 0;
}
Dereferencing a null pointer has undefined behaviour. Anything can
happen. What you implementation does with this code is outside the
scope of the C++ language. There is certainly no reason to expect a C+
+ exception to be thrown. The only thing that will be caught in a
catch block is a C++ exception generated by a throw statement, and
there isn't a throw statement in your code.

Given the right settings, your implementation *may* offer, *as an
extension to the language* (and so, outside the scope of this
newsgroup), predictable behaviour in the event of dereferencing a null
pointer. Your implementation may use the word "exception" to describe
some aspect of that behaviour. However, that is not the same thing as
a C++ exception.

Whether your implementation provides such an extension, how to use it
if it does, and whether it hooks into the C++ try-throw-catch syntax
in some way are all questions that are off-topic in this group. You
need to look in your compiler documentation and ask about it in a
compiler-specific newsgroup. Some suggestions in the FAQ:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.9

Gavin Deane

Jun 23 '07 #2
On 2007-06-23 10:46, mos wrote:
Hi!
When call a null function pointer will cause a execption, but it can't
be caught as std::exception, then which exception it is?
No, not an exception, it's an error. I would guess that it will result
in a segfault. What I do know is that it will terminate the application,
you can not recover from this.

--
Erik Wikström
Jun 23 '07 #3

"mos" <mm*******@163.comwrote in message
news:f5**********@news.cn99.com...
Hi!
When call a null function pointer will cause a execption, but it can't
be caught as std::exception, then which exception it is?
The following code is a example:

typedef void (*testfunc)(int a);

void test()
{
testfunc func = NULL;
func(10);
iTest* p = NULL;
p->Test(10);
}

int main()
{
try
{
test();
}
catch(std::exception& x)
{
std::cout << "exception: " << x.what() << std::endl;
}
catch(...)
{
std::cout << "unknown" << std::endl;
}
return 0;
}

this is a windows-only feature.
I think it is a useful feature and I don't understand why it is not in the
standard.
Some UNIX gurus probably thought that their signal() implementation is the
best what can be achieved.
On Windows a structured exception will be thrown -- assuming you have
structured exceptions enabled in your compiler.
You can overload the type of exception thrown by setting a handler using the
_set_se_translator() function.
In this handler you would have to throw a C++ Exception yourself -- ideally
a different one for every different code.

Jun 23 '07 #4
On 23 Jun, 17:27, "Peter" <junk189...@sbcglobal.netwrote:
"mos" <mmosqu...@163.comwrote in message

news:f5**********@news.cn99.com...
Hi!
When call a null function pointer will cause a execption
this is a windows-only feature.
I think it is a useful feature and I don't understand why it is not in the
standard.
Probably because in general, requiring defined behaviour on
dereferencing a null pointer would imply some runtime overhead on
every pointer dereference, which would go against the C++ philosphy of
"you don't pay for what you don't need".

If you want specific behaviour on dereferencing a null pointer, you
can write your own class that wraps a pointer and checks its value on
dereferencing. If an attempt is made to dereference a null pointer,
you can write whatever behaviour you want in your wrapper class (e.g.
throw an exception).

C++ lets you choose between that control, with its associated overhead
every time a wrapped pointer is dereferenced, and undefined behaviour,
which doesn't have the overhead.

Gavin Deane

Jun 23 '07 #5
On 2007-06-23 19:00, Gavin Deane wrote:
On 23 Jun, 17:27, "Peter" <junk189...@sbcglobal.netwrote:
>"mos" <mmosqu...@163.comwrote in message

news:f5**********@news.cn99.com...
Hi!
When call a null function pointer will cause a execption
this is a windows-only feature.
I think it is a useful feature and I don't understand why it is not in the
standard.

Probably because in general, requiring defined behaviour on
dereferencing a null pointer would imply some runtime overhead on
every pointer dereference, which would go against the C++ philosphy of
"you don't pay for what you don't need".
And I'm not sure if it's even possible to implement (without the
wrapper) on all architectures, I think it requires virtual memory (have
not given it much thought tough).

--
Erik Wikström
Jun 23 '07 #6

"Gavin Deane" <de*********@hotmail.comwrote in message
news:11**********************@n2g2000hse.googlegro ups.com...
On 23 Jun, 17:27, "Peter" <junk189...@sbcglobal.netwrote:
>"mos" <mmosqu...@163.comwrote in message

news:f5**********@news.cn99.com...
Hi!
When call a null function pointer will cause a execption
this is a windows-only feature.
I think it is a useful feature and I don't understand why it is not in
the
standard.

Probably because in general, requiring defined behaviour on
dereferencing a null pointer would imply some runtime overhead on
every pointer dereference, which would go against the C++ philosphy of
"you don't pay for what you don't need".

it does not require runtime overhead.
The only thing which is needed is that one can throw from a signal handler.

Jun 23 '07 #7

mos <mm*******@163.comwrote in message...
Hi!
When call a null function pointer will cause a execption, but it can't
be caught as std::exception, then which exception it is?
The following code is a example:
// --------------------------------------
#include <stdexcept>
class MyErr : public std::runtime_error { public:
MyErr( std::string const &msg = "") : std::runtime_error(msg){}
};
// --------------------------------------
typedef void (*testfunc)(int a);
void test() throw(MyErr){
testfunc func = NULL;
func(10);
iTest *p = NULL;
if( not p ){
throw MyErr(__FILE__": my message");}
}
p->Test(10);
}

int main(){
try{
test();
}
catch( MyErr const &x){
std::cout<<" caught MyErr: "<< x.what() <<std::endl;
} // catch(MyErr&)
catch(std::exception& x){
std::cout << "exception: " << x.what() << std::endl;
}
catch(...){
std::cout << "unknown" << std::endl;
}
return 0;
}
--
Bob R
POVrookie
Jun 23 '07 #8
Peter wrote:
:: "Gavin Deane" <de*********@hotmail.comwrote in message
:: news:11**********************@n2g2000hse.googlegro ups.com...
::: On 23 Jun, 17:27, "Peter" <junk189...@sbcglobal.netwrote:
:::: "mos" <mmosqu...@163.comwrote in message
::::
:::: news:f5**********@news.cn99.com...
::::: Hi!
::::: When call a null function pointer will cause a execption
:::: this is a windows-only feature.
:::: I think it is a useful feature and I don't understand why it is
:::: not in the
:::: standard.
:::
::: Probably because in general, requiring defined behaviour on
::: dereferencing a null pointer would imply some runtime overhead on
::: every pointer dereference, which would go against the C++
::: philosphy of "you don't pay for what you don't need".
::
::
:: it does not require runtime overhead.
:: The only thing which is needed is that one can throw from a signal
:: handler.

And that requires that dereferencing a null pointer invokes a signal
handler. On systems where this doesn't happen, there will be a huge
runtime overhead if they are required to check all pointer references
anyway.
Bo Persson
Jun 23 '07 #9
mos wrote:
When call a null function pointer will cause a execption, but it can't
be caught as std::exception, then which exception it is?
C++ doesn't throw null pointer exceptions, you're thinking of a higher-level
language like Java, C# or managed C++.

--
Dr Jon D Harrop, Flying Frog Consultancy
The OCaml Journal
http://www.ffconsultancy.com/product...ournal/?usenet
Jun 26 '07 #10

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

Similar topics

16
by: cody | last post by:
I have a method that gets called if the user presses a certain button. If the object is in a state that doesn't allow the calling of that method, what should I do? Should I better throw an...
13
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
2
by: André Nogueira | last post by:
Hi there! I am developing an Windows Explorer-like application that will tell you the size of any folder. But so far I have come across some problems. For instance, you may not have access to all...
11
by: l.woods | last post by:
I want to set up my CATCH for a specific exception, but I really don't know which one of the multitude that it is. I am getting the exception now with Catch ex as Exception but I want to be...
9
by: Mr Flibble | last post by:
Hi all, happy Friday! (certainly Friday is a day worth celebrating). I have a question on try/catch design (an exciting Friday topic for sure): I can either put a try/catch block in every...
4
by: J055 | last post by:
Hi I have 2 update buttons in my FormView ('Apply' and 'OK'). I want both buttons to update the data source but the 'OK' button should redirect afterwards. I can see which button is clicked...
10
by: jimmy | last post by:
Hi again, sorry for posting two questions so close together but im working on a school project which is due in soon and running into some difficulties implementing the database parts. I have the...
4
by: QC | last post by:
Hi Friends, I have one Application running on Client PC, i coded to store all Debug, Info, and Trace related information in Log file. This log file helps me to analyze the exception if any...
1
by: Brett | last post by:
I have a DropDownList in an ASP.NET web form that is populated with items from a lookup table by binding that DropDownList to a SqlDataSource. However, the items in the lookup table can change over...
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: 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
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.