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 11 34757
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
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 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.
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
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
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.
"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.
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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 ("...
|
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...
|
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
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |