473,698 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to catch all runtime exception

Hi,

I write some code guarded with exception handling...
simplified code look like this...
int main(int argc, char* argv[]){
try{

// code that might be throw runtime exception
// .....
// .....
}
catch(...){

}

return 0;
}

but when I ran the code, how come the code throw uncaught runtime
exception ?
I think the catch(...) block should catch all runtime exception ...
Or am I wrong ?

Thanks in advance

Nov 22 '05 #1
4 13681

chris wrote:
Hi,

I write some code guarded with exception handling...
simplified code look like this...
int main(int argc, char* argv[]){
try{

// code that might be throw runtime exception
// .....
// .....
}
catch(...){

}

return 0;
}

but when I ran the code, how come the code throw uncaught runtime
exception ?
I think the catch(...) block should catch all runtime exception ...
Or am I wrong ?
Not sure what you mean by "runtime exception". "runtime_error" s
(range_error etc) will be caught by catch(...). If you are talking
about floating point exceptions etc, they are asyncronous events and
hence are not handelled by C++ exception handling mechanism.

Posting the actual code might help.
Thanks in advance


Nov 22 '05 #2
Thanks Neelesh Bodas,

So, there's no way in C++ I can handle any runtime exception ?
Anyway, I'm coming from Java environment, and all exception
inherited from Exception class, so I can caught all runtime
exception. the point is, how can I prevent my code to "explode",
caused by uncaught exception.

Nov 22 '05 #3
* chris:
Thanks Neelesh Bodas,

So, there's no way in C++ I can handle any runtime exception ?
Anyway, I'm coming from Java environment, and all exception
inherited from Exception class, so I can caught all runtime
exception. the point is, how can I prevent my code to "explode",
caused by uncaught exception.


In C++, if you have an integer divide by zero, say, you have a bug that
should be fixed.

There is no guarantee of exceptions for bug manifestations.

Neither is there such a guarantee in Java, but in Java you have a better
chance of suppressing the most common _immediate_ effects of bugs, the
Ostrich policy of "what I don't see doesn't exist", until the bugs have
multiplied & migrated to the point where the code can't be rescued.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '05 #4
I once wrote a C++ class that convert OS signals to exceptions. The
problem is what to do when you catch the signal, as Alf said, when you
encounter a bug you should fix it. But if you have a program that does
large iterations and kind of clean itself between iterations it might
be helpfull:

#ifndef SIG_EXCEPT_H
#define SIG_EXCEPT_H

/*************** *************** *************** *************** *************** ****
File name : sigexception.h
Programmer : Yuval Lifshitz
Date : 03-sep-2003
Description : Try/Catch mechanism for OS signals.
Usage example:

TRY {
// write code here
}

CATCH {
// write code here
ex.WriteMsg();
cout << "signal number = " << ex.getSignal() << endl;
}

Notes: (1) This mechanism is NOT thread safe.
(2) The CATCH macro must follow immediatly after the
closing brackets of the TRY macro
(3) inside the CATCH block there exists a local variable
named ex holding the thrown signal data.

Change log : 23-sep-2004 make gcc compatible
add thread index hashing
*************** *************** *************** *************** *************** ****/

// include files
#include <stack>
#include <iostream>
#include <pthread.h>
#include <signal.h>
#include <setjmp.h>

// signal description list - used at the psignal function
extern const char *const sys_siglist[];

// jump function type
extern "C" {
typedef void (*jump_func_t)( int);
}

// main class definition
class TryCatchAsync
{
public:

// const and enum
enum {MAX_THREAD_NUM = 100};

//---------------------------
// internal class/type definitions
//---------------------------

//---------------------------------------------------------------
// Class Name : sigjmp_containe r
// Description : Container to encapsulate the sigjmp_buf type.
// This is done for the conviniece of allocation,
// since the original type is an array of integers.
//----------------------------------------------------------------
class sigjmp_containe r {
private:
sigjmp_buf m_sigbuf;

public:
sigjmp_containe r() {}
~sigjmp_contain er() {}

operator sigjmp_buf * ()
{
return &m_sigbuf;
}

sigjmp_buf * get_buf()
{
return &m_sigbuf;
}
};

//-----------------------------------------------
// Class Name : Exception
// Description : The exception thrown by the try
//-----------------------------------------------
class Exception {
public:
Exception(int sig) : m_sig(sig) {}

int getSignal() const
{
return m_sig;
}

void WriteMsg() const
{
psignal(m_sig, "signal thrown");
}

private:

int m_sig;
};

// jump adresses stack type
typedef std::stack<sigj mp_container> jump_stack_t;

// the constructor is used to generate the array of jump functions
// the object should be instanciated only once - but has no effect
// if instanciated more then once, since the relevant execution
// is done in compile time

// constructor
TryCatchAsync() ;

// destructor
~TryCatchAsync( ) {}

// static member functions
static void SigMaskAdd(int sig);
static void Push(sigjmp_con tainer &curr_addr);
static void Throw();
static int GetThreadIndex( );

// member functions
template <int func_id>
static void Jump(int sig)
{
pthread_t pt = GetThreadIndex( );
m_sig[pt] = sig;
siglongjmp(*(si gjmp_buf *)m_addr[pt].top().get_buf( ), 1);
}

private:

// data members
static int m_sig[MAX_THREAD_NUM];
static jump_stack_t m_addr[MAX_THREAD_NUM];
static jump_func_t m_jump_func[MAX_THREAD_NUM];
};

//-----------------------------------------------
// Class Name : JUMP_FUNC_GENER ATOR
// Description : a template struct that instanciate
// an array of different "Jump" functions.
// Note: this is done in a "generative programming"
// method in compile time.
//-----------------------------------------------
template <int N>
struct JUMP_FUNC_GENER ATOR
{
JUMP_FUNC_GENER ATOR(jump_func_ t *jump_func_arr)
{
jump_func_arr[N-1] = TryCatchAsync:: Jump<N-1>;
JUMP_FUNC_GENER ATOR<N-1> tmp_creator(jum p_func_arr);
tmp_creator = tmp_creator; // to avoid "unusead variable" warning
}
};

// termination condition for the generative process
template <>
struct JUMP_FUNC_GENER ATOR<0>
{
JUMP_FUNC_GENER ATOR(jump_func_ t *jump_func_arr)
{
jump_func_arr[0] = TryCatchAsync:: Jump<0>;
}
};

//------------------
// macro definitions
//------------------

#define SIG_INIT \
TryCatchAsync:: SigMaskAdd(SIGF PE); \
TryCatchAsync:: SigMaskAdd(SIGS EGV); \
TryCatchAsync:: SigMaskAdd(SIGI LL); \
TryCatchAsync:: SigMaskAdd(SIGB US);
// note: new signals should be added in the SIG_INIT macro

#define TRY SIG_INIT try { TryCatchAsync:: sigjmp_containe r curr_addr;\
if (sigsetjmp(*(si gjmp_buf *)curr_addr, 1) == 0) { \
TryCatchAsync:: Push(curr_addr) ; \
} \
else { \
TryCatchAsync:: Throw(); \
}

#define CATCH } catch(TryCatchA sync::Exception &ex)

#endif // RSEXCEPT_H

Nov 22 '05 #5

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

Similar topics

1
4430
by: DelboyJay | last post by:
I have found a strange resource problem, but luckily I have also found the solution which was not obvious from the runtime exception. I really think that the compiler should pick up this problem and fix it. What do you people think? Try the following: 1. Create a new c# windows application called MyTestApp 2. Create a notifyIcon on the form 3. Attach any icon you wish to the notifyIcon 4. Build and run the program to confirm that it...
3
1873
by: eas | last post by:
How to catch the following exceptions? Failure in an assignment operator Detection of an out-of-bound index in a user-defined array Range-checked access to a vector Any comments are appreciated!
1
2250
by: SHC | last post by:
Hi all, I did the "Build" on the attached code in my VC++ .NET 2003 - Windows XP Pro PC. On the c:\ screen, I got the following: Microsoft Development Environment An unhandled exception of type 'System.Xml.XmlException' occured in system.xml.dll Addtional Information: System error |Break| |Continue| I clicked on the |Continue| and I got the following: "c:\Documents and Settings\Scott H. Chang\My Documents\Visual Studio...
7
1796
by: Dan Bass | last post by:
In a somewhat complex application, I've developed plug-in architecture and am having a problem as to when to catch general exceptions for logging purposes. In each plug-in class library, for example, I'm assuming it's throw on errors, and that the core then catches them. I've got proxy classes handling some fiddly details of the plug-ins. Should I allow the exception to bubble to the top most point it can in a call stack, then catch...
3
1850
by: will | last post by:
Hi all. I've got an question about how to catch an exception. In Page_Load, I place a DataGrid, dg1, into edit mode. This will call the method called GenericGridEvent. GenericGridEvent will call a mehtod called ExceptionGenerator that will generate a run-time exception. The exception is caught in GenericGridEvent, but it isn't caught in InitializeComponenet (which doens't suprise me). I can't figure out where to catch an error that may...
1
1174
by: Paul Wu | last post by:
Suppose I have a form (frmMain) and a class (CheckStatus) that is instantiated on Form_Load. A button on the frmMain calls the CheckStatus.StartTimer method. The timer fires every second and calls the CheckStatus.Timer_Tick method. The code inside Timer_Tick is wrapped in a Try Catch block. Let's say something goes wrong inside Timer_Tick and an exception is properly thrown. Since the Timer_Tick is fired independently of the execution...
3
4649
by: IAMDkg | last post by:
Hi there - Can somebody give me some pointers on how I can create a XSLT which will compile fine but will thow a runtime exception during runtime. I tried to do the following but it doesn't throw any runtime exception. <?xml version='1.0' encoding='utf-8'?> <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/ Transform'>
3
26114
by: yinglcs | last post by:
I read the document here about exception handling in python: http://www.diveintopython.org/file_handling/index.html Can you please tell me how can I catch all exception in python? like this in Java: try { .... } catch (Throwable t) { ...
12
1928
by: Angus | last post by:
Hello I have a class I am using which raises an exception in its constructor if certain things aren't in place. I can easily create the situation where an exception is raised. If I use the create a member variable in a class using this class then how do I catch the exception? For now I have defined a member function as a pointer and in my
0
8674
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8861
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7728
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.