473,763 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Forcing exception handling

hi

in java i found that when a method has a throws clause in the definition,
callers must either handle the exceptions thrown by the method they are
calling or "forward" the exception to the caller by specifying a throws
clause as well.

is there a similar machanism in c++? i want to force a developer to write
handlers for all possible exceptions a method of my class library can throw.
thanks
bruno
Jul 19 '05
12 3693
you are right, i cannot prevent everything, but i want to prevent exceptions
thrown within my code to cause serious application errors or crashes. if the
os fails (i mean really fails), then of course i cannot to too much.

if i force handling or "forwarding " of an exception, it is a way of "asking"
a developer "are you aware of that possible error case?"

when forcing exception handling i know everywhere in the code what errors
could occur so i can prepare the code for proper handling.
"Alexander Terekhov" <te******@web.d e> wrote in message
news:3F******** *******@web.de. ..

"Ritz, Bruno" wrote:

it is not about "to exercise some authority". ever had the idea that i had the stability of the application in mind?

i just do not want uncaught exceptions.


How about terrorists or natural disasters? You can't really "force"
someone to prevent them, I'm afraid.

http://article.gmane.org/gmane.comp....st.devel/22348
(Re: [boost::thread] locks, exceptions and assertions)

regards,
alexander.

Jul 19 '05 #11
"Ritz, Bruno" <br********@gmx .ch> wrote in message
news:3f******** @news.swissonli ne.ch...
why is that a useless mechanism?

"Jonathan Mcdougall" <DE************ ******@yahoo.ca > wrote in message
news:at******** *************** *********@4ax.c om...
in java i found that when a method has a throws clause in the definition,callers must either handle the exceptions thrown by the method they are
calling or "forward" the exception to the caller by specifying a throws
clause as well.


Which is a completly useless mechanism.
is there a similar machanism in c++?


Of course not.
i want to force a developer to write
handlers for all possible exceptions a method of my class library can

throw.

Comments would be best.
Jonathan


IMO, the best way of doing this is enclosing _all_ your code in a try block,
followed by a catch(...) clause. This way, if the code in the catch clause
is reached, you will know for sure that you have an unhandled exception.
E.g.:

#include <iostream>
#include "whatever.h "

#define UnhandledExcept ionHandler() UnhandledExcept ionHandler(__FI LE__,
__LINE__)
void UnhandledExcept ionHandler(cons t char* c, int n)
{
std::cout << "Unhandled eexception in file " << c << " on line " << n <<
'\n';
}
in your code do this:
void function()
{
try
{
//everything
}
catch (MyException1 e)
{
//handle it
}
catch (MyException2 e)
{
//handle it
}
// and so on
catch (...)
{
UnhandledExcept ionHandler();
}
}
HTH,
S. Armondi
Jul 19 '05 #12

Samuele Armondi wrote:
[...]
IMO, the best way of doing this is enclosing _all_ your code in a try block,
followed by a catch(...) clause.


You've been reading way too much Sutter, I guess.

http://article.gmane.org/gmane.comp....st.devel/20858
(Re: Exception handling... it's time to fix the
http://www.boost.org/more/error_handling.html)

regards,
alexander.
Jul 19 '05 #13

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

Similar topics

11
2885
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses return code (not generating error/exception) so it is more compatible with other programming language.
7
6007
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
2751
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech exception handling mechanism which will allow me to pass character information about an error and its location from lower-level classes. Can you please critique the following exception handling mechanism in terms of my requirements ?
2
2595
by: tom | last post by:
Hi, I am developing a WinForm application and I am looking for a guide on where to place Exception Handling. My application is designed into three tiers UI, Business Objects, and Data Access Layer. My questions is where should I put exception handling: 1) Should it be put in all significant methods in all layers? 2) Should I create an exception base class that will handle the errors and pass useful error messages to the user?
9
2538
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in the way of CPU usage), compared to the "normal" practise returning values. How true is this? Will using using exception handling, in general, be much less efficient than returning values, or less efficient at all? Just curious...
44
4226
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
7
6167
by: Mark Rae | last post by:
Hi, I wrote a Windows service for a client a few months ago, and the client has now asked me to modify it so that it shuts itself down under certain circumstances e.g. a catastrophic failure of the network, SQL Server connectivity problems etc. I've tried to explain that this probably isn't a very good idea nor is it even necessary because the service has sufficient error and exception handling code to take account of such situations...
2
1027
by: Steve | last post by:
Hi, Is their any way to setup VS such that it forces programmers to handle all exception (similar to Java). Thanks --
1
3109
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
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
9998
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8822
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...
1
7366
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
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
3
2793
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.