?
Solved

catching exceptions from library

Posted on 2011-09-30
5
Medium Priority
?
424 Views
Last Modified: 2012-06-21
Hello,

My main exe is trying to catch an exception that is thrown from a shared library.  There is an error message in the catch block but program crashes without any message. Both library and main exe  linked with "dinkum with exceptions" option.

I'm using
QNX 6.5.0 with GNU 4.4.2
Could you help me please?
0
Comment
Question by:vileda
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 36894052
Allowing exceptions to percolate module boundaries is a bad idea. The standard C++ exceptions (for example) allocate memory on the heap (they contain a the message returned by the member function 'what()').

Heap allocated in one module should not be destroyed in another since each module has its own heap. When you catch and handle the exception object it will be destroyed and any heap it's allocate will be deleted. If the exception comes from another module this is a problem waiting to happen.
0
 
LVL 29

Expert Comment

by:pepr
ID: 36894250
Is the crash really manifested via an exception?  There are also crashes where no exception is thrown.  They are more low-level situation.
0
 

Author Comment

by:vileda
ID: 36896083
It happens where I put the exception. when the throw line closed it works properly.
 
0
 

Author Comment

by:vileda
ID: 36896131
What is the alternative way the handle errors? "if" checks? Object oriented C++ books encougrage exception usage. Should I make a choice between OOP and shared object library?
0
 
LVL 40

Accepted Solution

by:
evilrix earned 2000 total points
ID: 36896518
Unfortunately, C++ is a standard but libraries are not. Libraries are not a C++ thing. If you are using static libs then there is no problem since all code is linked into one module. The issue is that when you link to shared libraries the goal posts change.

A shared library should avoid throwing C++ exceptions across the api boundary. As well as the issue with memory it should also be noted that the applicarion using a library may not even know how to handle C++ exceptions (maybe the consuming language isn't C++ for example).

Except for special cases, you should avoid exception percolation. You would also be best to code the library with C and not C++ linkage and have api functions return error codes.

In you consumer app you can code a C++ wrapper for this api that handles interfacing with the library; checking the result of function calls and throwing a C++ exception.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question