469,343 Members | 5,484 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

throws keyword missing in C#

Why isn't there a 'throws' keyword in C# like java -

i would like to declare a function and say -

public int addup(int a, int b) throws ArithmeticExceptio,
DivideByZeroException
{
...
...
}

just like java so that addup function only throws these two types of
exceptions

is there any alternate coding? and if its not been included then why?

i'm new to c#

Your views are eagerly awaited

Thanks

Vijay

Nov 16 '05 #1
11 34004
There is a throw keywrod, check out the msdn online at
http://msdn.microsoft.com/library/en...owStatement.as
p

Hope this helps

Publicjoe

C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
VB Tutorial at http://www.publicjoe.f9.co.uk/vbnet/vbnet.html
VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html
Mirrors at http://www.dowhileloop.com/publicjoe/ and
http://www.publicjoe.justbe.com/

Useful Articles at http://www.madsally.co.uk

<vi*******@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Why isn't there a 'throws' keyword in C# like java -

i would like to declare a function and say -

public int addup(int a, int b) throws ArithmeticExceptio,
DivideByZeroException
{
...
...
}

just like java so that addup function only throws these two types of
exceptions

is there any alternate coding? and if its not been included then why?

i'm new to c#

Your views are eagerly awaited

Thanks

Vijay

Nov 16 '05 #2
<vi*******@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Why isn't there a 'throws' keyword in C# like java -


Check out the try...catch...finally syntax
http://msdn.microsoft.com/library/de...ogether_PG.asp
Nov 16 '05 #3
Microsoft has decided against adding the Throws syntax which would force
the caller of the method to explicitly catch the exceptions it throws.

For clearification to the user of the methods, you could use XML
comments and add the exception(s ?) element. But this is purely
documentual and don't enforce coding.

--
Patrik Lwendahl [C# MVP]
http://www.lowendahl.net/ || http://www.cshrp.net
Please reply only to the newsgroup.

vi*******@yahoo.com wrote:
Why isn't there a 'throws' keyword in C# like java -

i would like to declare a function and say -

public int addup(int a, int b) throws ArithmeticExceptio,
DivideByZeroException
{
...
...
}

just like java so that addup function only throws these two types of
exceptions

is there any alternate coding? and if its not been included then why?

i'm new to c#

Your views are eagerly awaited

Thanks

Vijay

Nov 16 '05 #4
There is no "throws" in C# because C# doesn't have checked exceptions
(exceptions that must have handlers at compile time).

For example, in java, any class method that *could* produce an
IOException *must* have a try-catch block for it, or say "throws" which
means let the caller worry about it. If you don't do this, your code
won't compile. This is the difference.

At compile time, C# doesn't check/care if you handle exceptions or not,
so there's no need for a throws keyword. C# only cares when an
exception is actually thrown, not if it may be thrown.

This is how it works to the best of my understanding.

Hope this helps,
-Dave

Nov 16 '05 #5
vi*******@yahoo.com wrote:
just like java so that addup function only throws these two types of
exceptions

is there any alternate coding? and if its not been included then why?


The short answer is no, there are no checked exceptions in C#. The
designer of the language discusses this decision in this interview:

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

The nearest you can get is to use the <exception> tags in your XML
documentation, and distribute NDoc generated docs with your
code/assemblies so that other people can see which exceptions you throw
(which is exactly what MS do in the MSDN documentation). You can't rely
on the compiler to tell you about unhandled exceptions, however, like
you may be used to in java.

--
Remove SHOES to reply.
Nov 16 '05 #6
"Publicjoe" <mi**@publicjoe.co.uk> wrote in
news:#0**************@TK2MSFTNGP15.phx.gbl:
http://msdn.microsoft.com/library/en...ThrowStatement
.as


throw is C# is very different than throwS with an s in Java. C# AFAIK does
not have the equivalent of throws in Java. In C# and like most other
languages, throws is simply not needed.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
Nov 16 '05 #7
Vijay,

In addition to what was said before about whether or not there is a
throws construct similar to Java, I figured I could add a little bit to the
why.

One of the main reasons (if not the main and overriding one) is that
with checked exceptions, most people (at least from the perspective of those
creating C#) just resorted to writing "throws Exception" as a catch-all. As
code bases became larger and larger, it becomes more prohibitive as you add
different exceptions further down in your call stack.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
<vi*******@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Why isn't there a 'throws' keyword in C# like java -

i would like to declare a function and say -

public int addup(int a, int b) throws ArithmeticExceptio,
DivideByZeroException
{
...
...
}

just like java so that addup function only throws these two types of
exceptions

is there any alternate coding? and if its not been included then why?

i'm new to c#

Your views are eagerly awaited

Thanks

Vijay

Nov 16 '05 #8
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
"Publicjoe" <mi**@publicjoe.co.uk> wrote in
news:#0**************@TK2MSFTNGP15.phx.gbl:
http://msdn.microsoft.com/library/en...ThrowStatement
.as


throw is C# is very different than throwS with an s in Java. C# AFAIK does
not have the equivalent of throws in Java. In C# and like most other
languages, throws is simply not needed.


That suggests that it's some kind of limitation of Java that forced its
inclusion, whereas in fact it was designed as a *feature* of Java.
Whether or not it's a *good* feature is a matter of much debate, of
course.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
I agree with Jon. It was a feature of Java. Whether it worked well is a
moot point.

What I wish C# would provide is enough information in each assembly
that a tool such as FxCop could tell you what exceptions your methods
will throw, and complain when you haven't documented them all. I agree
with the C# architects that "throws" is not such a good idea,
particularly given .NET's intense focus on versioning, but being left
without any way of discovering what exceptions a method / property
might throw seems to me a huge oversight.

Nov 16 '05 #10
"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
I agree with Jon. It was a feature of Java. Whether it worked well is a
moot point.

What I wish C# would provide is enough information in each assembly
that a tool such as FxCop could tell you what exceptions your methods
will throw, and complain when you haven't documented them all.
In another post I made a similar point. A combination of code analysis and a
few well thought out attributes could provide an effective alternative. You
could actually have two FxCop rules, one requiring the documentation of
exceptions, and another effectively enforcing checked exceptions for the
Java fans.

If you coupled code & exception documentation analysis with a:
ThrowsAttribute(type[, description]) - To explicitly add an exception not
visible in code (e.g. created via Reflection or something)
I agree
with the C# architects that "throws" is not such a good idea,
particularly given .NET's intense focus on versioning, but being left
without any way of discovering what exceptions a method / property
might throw seems to me a huge oversight.

Nov 16 '05 #11
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
throw is C# is very different than throwS with an s in Java. C# AFAIK
does not have the equivalent of throws in Java. In C# and like most
other languages, throws is simply not needed.


That suggests that it's some kind of limitation of Java that forced its
inclusion, whereas in fact it was designed as a *feature* of Java.
Whether or not it's a *good* feature is a matter of much debate, of
course.


I didnt mean to imply that it was because of bad construction or lack - if
thats the way it came accross Im sorry. I simply meant that in C# - its not
needed becuase it doesnt have the same "concept".
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
Nov 16 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Frederico Caldeira Knabben | last post: by
7 posts views Thread by Corepaul | last post: by
33 posts views Thread by Snis Pilbor | last post: by
8 posts views Thread by Steven D'Aprano | last post: by
6 posts views Thread by Emre DNER | last post: by
17 posts views Thread by Christoph Zwerschke | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.