473,465 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Checked Exception

Hi,

Unlike Java, there is no Checked Exception in C#.
Is there any work around for this.

For example, I am declaring an interface and inside this interface I have a
function and I would like to force this function to throw an exception in
the implementation.

public interface IMyInterface
{
void foo () throws MyException;
}

how can I do that?

Thanks,
Lalit
Nov 15 '05 #1
4 1863

You can't, atleast not currently.
The best you can do is provide explicit documentation

"Lalit Parashar" <la****@mapleusa.com.NOSPAM> wrote in message
news:ei*************@tk2msftngp13.phx.gbl...
Hi,

Unlike Java, there is no Checked Exception in C#.
Is there any work around for this.

For example, I am declaring an interface and inside this interface I have a function and I would like to force this function to throw an exception in
the implementation.

public interface IMyInterface
{
void foo () throws MyException;
}

how can I do that?

Thanks,
Lalit

Nov 15 '05 #2
Yes. In the NAME. IMyInterfaceWithExceptions <g>

Regards,
Jeff
The best you can do is provide explicit documentation<


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3
Hi, in the example you gave in java, someone implementing the foo function
does not need to throw any exception you could do this:

public class MyClass implements IMyInterface
{
void foo ()
{
}
}

and it would compile fine... I haven't tried it but I'm pretty sure
Dominic

"Lalit Parashar" <la****@mapleusa.com.NOSPAM> a écrit dans le message de
news: ei*************@tk2msftngp13.phx.gbl...
Hi,

Unlike Java, there is no Checked Exception in C#.
Is there any work around for this.

For example, I am declaring an interface and inside this interface I have a function and I would like to force this function to throw an exception in
the implementation.

public interface IMyInterface
{
void foo () throws MyException;
}

how can I do that?

Thanks,
Lalit

Nov 15 '05 #4
As far as I can tell, this was a design decision by Microsoft. As a long
time Java programmer, I have come to think it is a good one. It is usually
a bad idea to try to force only a certain type of exception, and in fact it
doesn't work that way in Java anyway. RuntimeExceptions are not checked,
and lots of code can throw things like null-pointer exceptions or divide by
zero exceptions, and Java doesn't catch it. You never see these sorts of
things documented, but they can be thrown all over the place by all sorts of
code.

Usually what happens when you force the users of your library to use
specific exception types is that they have to catch all the Exceptions and
just rethrow them as your exception type, or as a RuntimeException. Silly,
and a waste of effort. Why would I want to wrap a divide-by-zero exception
inside a SocketException, for instance? It just obfuscates the source of
the error.

The other thing that happens is you end up writing stupid exception handlers
for exceptions that are declared but which never happen. If your code is
written in such a way that the exception is never going to occur, you still
have to catch the exception and deal with it because the compiler says so.
I remember that Xerces has one of those for the DocumentFactory, or one of
those objects. You call the function, it works every time. Never, ever
fails. Yet it supposedly could throw an exception, so you have to deal with
a meaningless try catch structure. Silliness.

That one feature in Java has caused as many arguments as whether VB6 can be
considered an OO language.

Let your exceptions run free! It really does work quite well that way.
Document and catch the exceptions you are interested in, and let the rest
bubble up the stack. If they are a problem, the framework will let you
know.

"Lalit Parashar" <la****@mapleusa.com.NOSPAM> wrote in message
news:ei*************@tk2msftngp13.phx.gbl...
Hi,

Unlike Java, there is no Checked Exception in C#.
Is there any work around for this.

For example, I am declaring an interface and inside this interface I have a function and I would like to force this function to throw an exception in
the implementation.

public interface IMyInterface
{
void foo () throws MyException;
}

how can I do that?

Thanks,
Lalit

Nov 15 '05 #5

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

Similar topics

6
by: Hung Jung Lu | last post by:
Hi, Just ran into this article http://www.mindview.net/Etc/Discussions/CheckedExceptions And I felt kind of reliefed. When I programmed in Java I always thought its usage of "checked...
26
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...
9
by: F. GEIGER | last post by:
Hi everybody, I like checked exceptions as provided by Java. No, I don't want to start a thread on that, but ask, if anyone knows of a tool for that. It may be stand-alone, but being plugable...
9
by: Jeff Louie | last post by:
I drank too much coffee last night and came up with a suggestion about how to add checked exceptions to C# http://www.geocities.com/jeff_louie/OOP/oop14.htm Comments expected <g> Regards,...
26
by: Simon Jefferies | last post by:
Hello, I am trying to add an item to a checked list box, like: clbList.Items.add("Hello",true) I get an error back: Run-time exception thrown: System.ArgumentOutOfRangeException -...
12
by: Ioannis Vranos | last post by:
Perhaps a mechanism can be introduced in the C++0x/1x standard, something simple like defining a function as: void somefunc(void) throw() { // ... }
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
1
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.