Click to See Complete Forum and Search --> : Does c# have 'throws' keyword?


zhang_shuigang
January 10th, 2006, 08:48 PM
Hi,

I use some existing code,packaged in a dll file.I want my code to be robust,so in places where existing code throws a exception,I want to handle it.
The problem is,I donot know which methods in existing code will throw a exception!I donot have access to the source code,and the document is poorly written.


My question is:
1) In c#,How to declare that a method may throw some exceptions,except by document and comment? Is there a counterpart of java's 'throws'?Or what can I do to mimic 'throws'.

2) In my scenario,what can I do to protoct my code.

I am sorry to ask this question,I am sure somebody has asked it before!

Thanks.

MadHatter
January 10th, 2006, 09:07 PM
no throws in C#. as far as I've seen, its just a function of documentation.

one thing you can do (and is a good idea anyway) is to write unit tests for that library & your code inserting tests for each use case for your software. this way you know that your code will be somewhat safe.

zhang_shuigang
January 10th, 2006, 10:28 PM
Bad news!

No documation,no source code,no test code.
What I can do now is guessing,and when something go wrong,fix it.

This is really bad!
Even if I decide to wrap every method call with try-catch(ugly),I have no idea what kind of error can be thrown.

Any suggestion?

Thanks.

MadHatter
January 10th, 2006, 10:46 PM
do you know how to use it? if you dont I wouldnt recomend using it. if you do, write your own unit tests, and make it fail to see what exceptions are thrown. otherwise, only handle those you know about and dont worry about those you dont.

dont wrap every method in a try / catch. thats just bad news anyway you look at it. write unit tests and be done with it. nunit is like junit http://www.nunit.org/, I'd suggest using it.

boudino
January 11th, 2006, 01:47 AM
If you have no documentation, something like throws woldn't help you much. If you mean throws similar to Java's, it allows you yo only know that an exception is raised, but tells nothing about the nature of the exception - what does it mean.

You can apply try try/catch catching generic Exception to every call to methodsof the dll, or create a facade (facade design pattern) over the dll and apply try/catch to calls to the facade.

If the dll is .net assembly (as I suppose), you could also try disassembly it into IL and look into the IL for place where throw occures.

Norfy
January 11th, 2006, 01:58 AM
Have you tried pointing Reflector (http://www.aisto.com/roeder/dotnet/)at the assembly? This will decompile it and show you source code.

I would also go with MadHatter and write NUnit tests that exercise the API of the assembly so you know what it does and what exceptions get thrown.

klintan
January 11th, 2006, 02:09 AM
This is why c# not has checked exceptions:

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

zhang_shuigang
January 11th, 2006, 05:34 AM
Thanks!

I will decompile the dll with Reflector.And,learn to use nunit.


I think,in some case,I DO NEED a way to tell my client that my codes can break,programmatically.My client might ignore these exceptions,but they should be warned about this.

Maybe c# compiler should provide a switch option,you can turn this switch on if you want to handle exceptions.

boudino
January 11th, 2006, 06:37 AM
If it is your own code, you can use attributes to tag method which can throw an exception. Then you can look for to the attributed in your code (to decide which branch to go - with try/catch or without) or you can use it FxCop to look for places, where methodst with the attribute are not directly closed in try/catch.