473,804 Members | 3,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exceptions part of function signature

Hi,

Is it an option/idea to make the exceptions thrown be part of the
signature of a function?
- So I'll get a warning if I call a function and not handle one of the
exceptions.
- So I'll get an error if I make a derived version and throw new kind of
exception.

Gerard van Dorth
Nov 21 '05 #1
5 1388
"Gerard" <ge************ **@hcc-net.nl> schrieb:
Is it an option/idea to make the exceptions thrown be part of the
signature of a function?
- So I'll get a warning if I call a function and not handle one of the
exceptions.
- So I'll get an error if I make a derived version and throw new kind of
exception.


A similar concept was used in Java (checked exceptions) but has not been
introduced to .NET for some good reasons. The article below describes these
reasons, although it's written for C#, it applies to VB.NET too:

Why doesn't C# have exception specifications?
<URL:http://msdn.microsoft. com/vcsharp/team/language/ask/exceptionspecs/default.aspx>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #2
Herfried,

I was reading the same page, and thought, that is not completly what the OP
asked.

And therefore was not showing the link.

:-)

Cor
Nov 21 '05 #3
Cor,

"Cor Ligthert" <no************ @planet.nl> schrieb:
I was reading the same page, and thought, that is not completly what the
OP asked.


Huh?! It's exactly part of what the OP asks:

| Is it an option/idea to make the exceptions thrown be part of the
| signature of a function?

No, it's a bad idea because something because its foundation, checked
exceptions, are a bad idea. So, adding checked exceptions is a bad idea,
and making exceptions part of the signature is even worse because, for
example, overloading based on the thrown exceptions wouldn't work.

| - So I'll get a warning if I call a function and not handle one of the
| exceptions.

In Java, code won't compile if you don't handle a checked exception. The
reasons why this does not make much sense are shown in the article I
referenced (versioning, ...).

| - So I'll get an error if I make a derived version and throw new kind of
| exception.

I didn't check how this is solved in Java, but I think this issue is another
problem of checked exceptions.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #4
Herfried,

- Versioning: though true, this holds for more properties of the
signature (so, not a strong argument)

- Code quality: if A calls B, B calls C, C calls D, and D raises an
exception that is eventually handled by A, A normally doesn't know anything
about the context of the exception and hence can't handle "program state"
properly.

- If the number of possible exceptions could be large, so make
checked exceptions optional.

- Impracticality of having class author differentiate between
"checked" and "unchecked" exceptions: If A calls B and B doesn't do the
things it should do, it better raises a checked exception.

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:uZ******** ******@TK2MSFTN GP11.phx.gbl...
"Gerard" <ge************ **@hcc-net.nl> schrieb:
Is it an option/idea to make the exceptions thrown be part of the
signature of a function?
- So I'll get a warning if I call a function and not handle one of the
exceptions.
- So I'll get an error if I make a derived version and throw new kind of
exception.


A similar concept was used in Java (checked exceptions) but has not been
introduced to .NET for some good reasons. The article below describes
these reasons, although it's written for C#, it applies to VB.NET too:

Why doesn't C# have exception specifications?
<URL:http://msdn.microsoft. com/vcsharp/team/language/ask/exceptionspecs/default.aspx>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5
"Gerard" <ge************ **@hcc-net.nl> schrieb:
- Versioning: though true, this holds for more properties of the
signature (so, not a strong argument)
I think it's a tradeoff. In the time of the Java boom I didn't like checked
exceptions because they made everything more complicated without adding more
value. What other properties of a signature add less value than checked
properties would do?

In addition, checked exceptions would not increase code quality, simply
because the avarage programmer would catch it instead of propagating it
using 'throws...' in the procedure's head, and, to make it even worse,
assign an empty exception handler. Checked exceptions always were a
controversial feature in the Java community and subject of discussion.
You'll find many articles on the web discussing checked exceptions and if
they are valuable enough to be added to a programming language. An
interesting article by Bruce Eckel (who authored books about Java) can be
found here:

Does Java need Checked Exceptions?
<URL:http://www.mindview.ne t/Etc/Discussions/CheckedExceptio ns>

The last comment on the page referenced above quotes Eric Gunnerson
(<URL:http://discuss.develop .com/archives/wa.exe?A2=ind00 11A&L=DOTNET&P= R32820>),
who was one of the designers of C#:

| Examination of small programs leads to the conclusion
| that requiring exception specifications could both enhance
| developer productivity and enhance code quality, but experience
| with large software projects suggests a different result --
| decreased productivity and little or no increase in code quality.

Time has shown that checked exceptions were a bad idea, and thus they were
not included. There was for sure a long process of discussion going on
before this decision.
- Code quality: if A calls B, B calls C, C calls D, and D raises
an exception that is eventually handled by A, A normally doesn't know
anything about the context of the exception and hence can't handle
"program state" properly.
In this case, handle the exception in 'C'. My daily developer experience
shows me that I always keep the documentation opened to lookup the
exceptions thrown by methods I use in my code. I think there are more
evolved concepts to increase code quality than checked exceptions. Checking
for empty exception handlers (using tools like FxCop), using automated tests
(unit testing) and better training are far more effective without affecting
versioning, for example.
- If the number of possible exceptions could be large, so make
checked exceptions optional.


That's what Java does, IIRC.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #6

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

Similar topics

21
2239
by: dkcpub | last post by:
I'm very new to Python, but I couldn't find anything in the docs or faq about this. And I fished around in the IDLE menus but didn't see anything. Is there a tool that can determine all the exceptions that can be raised in a Python function, or in any of the functions it calls, etc.? /Dan
26
2920
by: OvErboRed | last post by:
I just read a whole bunch of threads on microsoft.public.dotnet.* regarding checked exceptions (the longest-running of which seems to be <cJQQ9.4419 $j94.834878@news02.tsnz.net>. My personal belief is that checked exceptions should be required in .NET. I find that many others share the same views as I do. It is extremely frustrating to have to work around this with hacks like Abstract ADO.NET and CLRxLint (which still don't solve the...
24
2505
by: mag31 | last post by:
Is there any way to find out if a particular .net function will throw an exception without first generating the exception? I am using structured exception handling i.e. try catch finally blocks with a top level catch all for Exception. However, I would like to be able to catch most .net exceptions when they are generated. I would then be able to generate a valuable exception message and do something about it!!! Hence the question above....
9
2342
by: Gianni Mariani | last post by:
I'm involved in a new project and a new member on the team has voiced a strong opinion that we should utilize exceptions. The other members on the team indicate that they have either been burned with unmaintainable code (an so are now not using exceptions). My position is that "I can be convinced to use exceptions" and my experience was that it let to code that was (much) more difficult to debug. The team decided that we'd give...
5
1247
by: Ian Lazarus | last post by:
Hello, Are you aware of any tool which analyzes source code and documents the exceptions thrown for each routine in each source file for a project? Thanks
15
426
by: Bernard | last post by:
Hi All, I am not sure if I should be asking this question on clc or clc++. Let me try on both. I hope that this is not too trivial for the brilliant minds over here. I know that OOP questions have been asked on clc before so it is probably OK. I am a newbie to C++. BS 3rd edition states: % The throw transfers control to a handler for exceptions .... %
8
2260
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an exception should be thrown only in exceptional situations. It turned out that each of my colleagues had their own interpretation about what an "exceptional situation" may actually be. First of all, myself I’m against using exceptions extensively,...
10
3911
by: Josefo | last post by:
Hello. Here I am again trying to understand the examples of a C++ tutorial. When compiling the original source code: // standard exceptions #include <iostream> #include <exception> using namespace std; class myexception: public exception {
8
2080
by: wrungel | last post by:
Exceptions thrown by C++ function which is called by a C-function which is called by a C++ function are not catched by outermost C++ function. Operating system: Linux Compiler: GNU GCC Version 4.2.1 file x.cc: main () {
0
9706
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
9579
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
10575
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...
1
10319
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
10076
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...
1
7616
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
5520
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...
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
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.