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

How to prevent compiling when an exception has not been handeled

Hi,
As you know in Java, when a method is able to throw an exception the
programmer has to handle the exception otherwise the program will not
compile.
Is there anyway to do so in C#. I mean generate an error message for
each unhandled exception?
Thanks.

Aug 9 '07 #1
9 1276
shahoo wrote:
As you know in Java, when a method is able to throw an exception the
programmer has to handle the exception otherwise the program will not
compile.
Is there anyway to do so in C#. I mean generate an error message for
each unhandled exception?
No.

C# does not have checked exceptions.

Arne
Aug 9 '07 #2
shahoo wrote:
Hi,
As you know in Java, when a method is able to throw an exception the
programmer has to handle the exception otherwise the program will not
compile.
Is there anyway to do so in C#. I mean generate an error message for
each unhandled exception?
No. C# doesn't provide a way to advertise to the compiler which
exceptions it could throw, so there's also no way for it to require that
calling methods always catch exceptions that could be thrown.

Not having done any Java programming myself, I had never heard of that
feature until someone else asked the same question a while back. I
still have mixed feelings regarding whether I'd want to use that sort of
requirement. On the one hand, it seems like a good idea with respect to
making sure you always know what kind of exceptions you might need to
handle. On the other hand, it sounds like it could lead to a lot of
busy work, as you repeatedly bubble up possible exceptions. :)

Pete
Aug 9 '07 #3
Peter Duniho <Np*********@NnOwSlPiAnMk.comwrote:

<snip>
Not having done any Java programming myself, I had never heard of that
feature until someone else asked the same question a while back. I
still have mixed feelings regarding whether I'd want to use that sort of
requirement. On the one hand, it seems like a good idea with respect to
making sure you always know what kind of exceptions you might need to
handle. On the other hand, it sounds like it could lead to a lot of
busy work, as you repeatedly bubble up possible exceptions. :)
Spot on. Checked exceptions are a somewhat failed experiment. I think
we may eventually want *something* like them, but not quite what Java
has. I used to be a big fan, but I'm not any more...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 9 '07 #4
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Peter Duniho <Np*********@NnOwSlPiAnMk.comwrote:

<snip>
>Not having done any Java programming myself, I had never heard of that
feature until someone else asked the same question a while back. I
still have mixed feelings regarding whether I'd want to use that sort of
requirement. On the one hand, it seems like a good idea with respect to
making sure you always know what kind of exceptions you might need to
handle. On the other hand, it sounds like it could lead to a lot of
busy work, as you repeatedly bubble up possible exceptions. :)

Spot on. Checked exceptions are a somewhat failed experiment. I think
we may eventually want *something* like them, but not quite what Java
has. I used to be a big fan, but I'm not any more...
This is an interview with Anders Hejlsberg where he explains why there are
no checked exceptions in C#.

http://www.artima.com/intv/handcuffs.html

- Michael S
Aug 9 '07 #5

On Aug 9, 7:51 am, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Checked exceptions [...] I used to be a big fan, but
I'm not any more...
I'm a big fan of the <exceptionXML comment tag. =]

Aug 9 '07 #6

On Aug 9, 3:52 pm, Samuel R. Neff <samueln...@nomail.comwrote:
[...] some implementations return -1 and others throw an exception.
[...] Checked exceptions would be a way to force compliance.
Sometimes returning -1 is just as legal as throwing an exception
((network) stream reading comes to mind), in which case checked
exceptions wouldn't really do anything for compliance. Carefully
thought-out design patterns do more to alleviate these problems than
do checked exceptions, _and_ don't come bundled with a hangover.

Aug 9 '07 #7

I don't agree that returning -1 and throwing an exception are both
legal when it's the same method of an interface, just different
concrete classes.

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Thu, 09 Aug 2007 19:26:55 -0000, UL-Tomten <to****@gmail.com>
wrote:
>
On Aug 9, 3:52 pm, Samuel R. Neff <samueln...@nomail.comwrote:
>[...] some implementations return -1 and others throw an exception.
[...] Checked exceptions would be a way to force compliance.

Sometimes returning -1 is just as legal as throwing an exception
((network) stream reading comes to mind), in which case checked
exceptions wouldn't really do anything for compliance. Carefully
thought-out design patterns do more to alleviate these problems than
do checked exceptions, _and_ don't come bundled with a hangover.
Aug 9 '07 #8

On Aug 9, 10:16 pm, Samuel R. Neff <samueln...@nomail.comwrote:
I don't agree [...]
Yeah, that's one of the reasons checked exceptions don't work in
practice right there. If we don't agree on the mechanics, compile-time
declarations aren't going to solve the original problem, which in this
case could be something like "I need to be certain I can gracefully
handle everything your code does".

Aug 9 '07 #9
I am a Java fan myself and I like that feature too. But of my bad luck
I have to do a project in C# for my university course.
BTW, thanks for the help.

Aug 10 '07 #10

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

Similar topics

4
by: Gopal-M | last post by:
I have the problem with sizeof operator I also want to implement a function that can return size of an object. My problem is as follows.. I have a Base class, say Base and there are many class...
20
by: modemer | last post by:
Question is as in subject. For example: class BaseClass { public: void func() { do something; } // I don't want this function being overloaded in its inherited class };
3
by: Travis | last post by:
I am trying to prevent an exception from bubbling up from one nested Try/Catch Block to its "Parent" Try/Catch Block. Here is some example code: try{ try{ //Non-application killing code that...
4
by: Aaron Queenan | last post by:
When I build a C++ library to .NET using the managed C++ compiler, I get the following error message: Linking... LINK : error LNK2020: unresolved token (0A000005) _CrtDbgReport LINK : error...
7
by: Leif Eirik Olsen | last post by:
Hi, I have a class Test with a property Active. Setter and getter is implemented. Setter ~ set { try { if (SomeValue) throw new Exception("Error");
12
by: Markus Ewald | last post by:
I'm just experimenting around with the VisualC++ 2005 Express Edition Beta and found some strange behavior for which I can't seem to find a workaround. What I've done is set up two static library...
4
by: Rob Richardson | last post by:
Greetings! I am working on an application that targets a Pocket PC running Windows CE and SQL Server CE. Almost all functions in the application use a Try block with a Catch block that looks...
3
by: Ryan Liu | last post by:
Can someone give a sample to prevent a row from being deleted in a datatable? I tried e.Row.RejectChanges(); in dt_RowDeleting() but seems does not work. I need verify if there other data...
12
kirara
by: kirara | last post by:
Hello, I am new to rmi and linux enviroment, I have been trying to test this code (java) on uinx where there are three machines: -One as a Controller -an execution host - a submitter I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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:
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
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,...

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.