473,394 Members | 1,715 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,394 software developers and data experts.

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 34757
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 Löwendahl [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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Frederico Caldeira Knabben | last post by:
Hello, I'm having a problem when trying to use the Directory.CreateDirectory() method. It throws a DirectoryNotFoundException exception. I'll try to illustrate it: Suppose you have the...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
33
by: Snis Pilbor | last post by:
With the "as if" rule in play, doesn't that effectively render the "register" keyword completely useless? Example: I make a silly compiler which creates code that goes out of its way to take a...
3
by: anupamadatta | last post by:
Hi there, I am trying to execute one Insert query using Pro C . the Insert query is as below: Prepared statement is : stmnt.len = sprintf((char*)stmnt.arr, "INSERT INTO %s ("...
8
by: Steven D'Aprano | last post by:
I'm writing a factory function that needs to use keywords in the produced function, not the factory. Here's a toy example: def factory(flag): def foo(obj, arg): if flag: # use the spam keyword...
6
by: Emre DÝNÇER | last post by:
does c# provide an alternative to the throws keyword in javA?so that my client developer will strictly implement the exceptions that my methods may throw? thanks in advance
17
by: Christoph Zwerschke | last post by:
I'm just reading PEP 3107 (function annotations) and wonder why exceptions are not mentioned there. I think it would be helpful if one could specify which exceptions can be raised by a function,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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,...
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...

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.