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

Should an overloaded, non-member operator throw?

Hi,

I've written a class called ConfigParser, which can be used to parse
config files. (Surprise!) I have overloaded the shift operators to
mimic the std iostreams. My question is, should overloaded operators
throw or not? Both are non-member friends of ConfigParser.

Cheers,
Ralph

--
Ralph Moritz
Quantum Solutions Ph: +27 315 629 557
GPG Public Key: http://ralphm.info/public.gpg
Dec 21 '06 #1
1 1466

Ralph Moritz wrote:
Hi,

I've written a class called ConfigParser, which can be used to parse
config files. (Surprise!) I have overloaded the shift operators to
mimic the std iostreams. My question is, should overloaded operators
throw or not? Both are non-member friends of ConfigParser.

Cheers,
Ralph
Sure, if you need them to throw, let them throw (ie:
std::runtime_error). As long as you use a try-catch block to capture
the thrown exception in context. Otherwise your program will call
terminate(). Silly example:

#include <iostream>
#include <ostream>
#include <stdexcept>

template< typename T >
class N
{
T m_t;
public:
N(T t) : m_t(t) { }
friend std::ostream&
operator<<(std::ostream& os, const N& r_n)
{
if(r_n.m_t 100 || r_n.m_t < 0)
throw std::runtime_error("op<<: out of range\n");
os << r_n.m_t;
return os;
}
};

int main()
{
try
{
N< int n(-1);
std::cout << n << std::endl;
}
catch( const std::exception& r_e )
{
std::cerr << "Error: " << r_e.what();
std::cerr << std::endl;
}
}

/*
Error: op<<: out of range
*/

If you plan to support a throw specification for the operator, which i
wouldn't recommend, use throw(std::runtime_error, std::bad_exception).

Dec 21 '06 #2

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

Similar topics

8
by: User | last post by:
Hi, This is very basic, It may be a repost, if so I'm sorry. The problem is that this declaration : Private strMyArray(100) As String will create an array of string with a length of 101,...
5
by: John Harrison | last post by:
I've tried the following program on two different compilers (VC++ 7.1 and gcc 3.3.1) and both print "first" when I expected "second". Why is that? #include <vector> #include <iostream> ...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
10
by: john bailo | last post by:
Can a web method be overloaded? To support varying numbers of input parameters?
11
by: sotto | last post by:
If i have this Interface: Public Interface MyInterface Function test() As Boolean Function test(ByVal MyVar As String) As Boolean End Interface And then i make a Public Class MyOwnClass
12
by: Jack Daly | last post by:
I've inherited some code which uses an undocumented feature of a third-party vendor's library. Essentially, this vendor has kept the details of an interface struct secret, but we can pass a pointer...
2
by: raylopez99 | last post by:
I'm having problems compiling complex reference declarations in MSVC++.NET 2002 IDE. Here is an example: // --Foo.h-- #include "Bar.h" class Bar; //forward decl. to a class Bar in...
28
by: steve yee | last post by:
i think c should adapt c++ template standard, as well as namespace. if so, c can replace c++ in many cases.
5
by: __PPS__ | last post by:
Hello everybody, I'm not sure but I thought that sometime ago I had class with two overloaded methods: void func(const char*); and template<int N>func(const char (&arr)); so, that whenever I...
3
by: mathieu | last post by:
Could someone please tell me what is wrong with the following -ugly- piece of c++ code. Why when I explicititely set the template parameter my gcc compiler start getting confused: bla.cxx: In...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
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...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.