I have been working on a conversion project for last few days where I need to convert a Java library to C#. At one point I came to java's certain feature called Exceptions Thrown by a Method. Here is a little glimpse of it:

public boolean isRequest() throws NullPointerException
   {  
    // blocks of code implementing isRequest() method;
   }

For the C# implementation of this code snippet, I searched the web and quite shockingly found that, C# does not support throwing exceptions in the compile time.

Now my question is, how can I implement this code portion in a C# library?

Recommended Answers

All 7 Replies

Congratulations! You're no longer a DaniWeb newbie.<br /> <br />
Your DaniWeb account has just been upgraded from newbie status and now you have the ability to take advantage of everything the community has to offer.<br /> <br />
You can now enjoy an advertisement-free DaniWeb by ticking the checkbox to Disable Ads in your profile. You will no longer have to fill out the human verification check when you post. You can also now send unlimited private messages, contribute new code snippets, and tag articles with never-before-used tags.

When I search throw exceptions c# through google, shockingly I get over 2,000,000 hits.

Here's a link that discusses the throw statment.

Here's a link that discusses exceptions and exception handling

Since no code is run during compiling the only exceptions that get thrown are from bad code.

@tinstaafl, its not about just throwing exception. In Java using this block(Exceptions Thrown by a Method, follow my link if u please) of code u can check for an exception on compilation time. In C# one cannot simply use a try block which will be executed in compile time, that's why I'm saying C# does not provide this feature by default. I need to know how I can implement it manually.

Thanks for your comment.

C# does not provide this feature by default.

I think C# does not have it [Click Here](C# does not provide this feature by default.) for a possible solution.
The links provided by tinstaafl should also be helpfull. Success!

C# does not have this type of construction.
Click Here for a possible solution.
The links provided by tinstaafl should also be helpfull.

thanks @ddanbe.

for the time being I have implemented a try-catch block inside the called function. no errors so far, let see what happens after the final build. :)

You will need a plugin or addon to add this functionality to Visual Studio.

Try Resharper.

As a programmer, you really should be aware of what code can throw an exception already. Most documentation will tell you and (especially in possible NullReference cases) you should be checking for null before hand.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.