views:

110

answers:

5

this keywords allows for a method to not handle an exception by its own, but rather throw it to the calling method. If there is no equivalent, how can you accomplish the same (or a similar) effect?

see this to read about java throws keyword / clause

+4  A: 

In Java, you must either handle an exception or mark the method as one that may throw it using the throws keyword.

C# does not have this keyword or an equivalent one, as in C#, if you don't handle an exception, it will bubble up, until caught or if not caught it will terminate the program.

If you want to handle it then re-throw you can do the following:

try
{
  // code that throws an exception
}
catch(ArgumentNullException ex)
{
  // code that handles the exception
  throw;
}
Oded
@Louis RH - I do understand. There is no C# (or .NET) equivalent.
Oded
"it will bubble up", does this means that this is equivalent to all methods having a throws clause in Java?
Louis Rhys
@Louis RH - kind of. It means an exception, if not handled, will go up the call chain through each calling function till handled.
Oded
@Louis RH not completely, that would mean you had to catch the Exception at least in you Main to compile the code. Because C# doesn't know checked exceptions it's up to you to catch them otherwise they will just land in the runtime and interrupt your code.
Johannes Wachter
`ex` is redundant
ventr1s
@jwatcher: a main method can have a throw clause too.
Louis Rhys
@ventr1s - it depends if you are doing something with it or not. As the code stands, yes, it is.
Oded
minor typo - "mark the class" - in Java you mark a method as throwing a checked exception, not the class
serg10
@serg10 - thanks for the correction.
Oded
+3  A: 

You are asking about this :

Re-throwing an Exception

public void Method()
{
  try
  {
        int x = 0;
    int sum = 100/x;
  }
  catch(DivideByZeroException e)
   {
   throw;
   }
}

or

static void Main() 
    {
        string s = null;

        if (s == null) 
        {
            throw new ArgumentNullException();
        }

        Console.Write("The string s is null"); // not executed
    }
Pranay Rana
+1 for using `throw`. By using it, the stack trace won't get lost.
Giu
+1  A: 

Here's an answer to a similar question I just fund on bytes.com:

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 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.

Andreas_D
A: 

Actually not having checked exceptions in C# can be considered a good or bad thing.

I myself consider it to be a good solution since checked exceptions provide you with the following problems:

  1. Technical Exceptions leaking to the business/domain layer because you cannot handle them properly on the low level.
  2. They belong to the method signature which doesn't always play nice with API design.

Because of that in most bigger applications you will see the following pattern often when checked Exceptions occur:

try {
    // Some Code
} catch(SomeException ex){
    throw new RuntimeException(ex);
}

Which essentially means emulating the way C#/.NET handles all Exceptions.

Johannes Wachter
I can't imagine how checked exceptions would mix with lambdas!
Gabe
@Gabe: I'm sure you could come up with some concept that allows you to mix them, but like I said, checked exceptions are in Java also mostly not good practice, especially in more complex applications. So it's good they are not there in C#.
Johannes Wachter
+2  A: 

The op is asking about the C# equivalent of Java's throws clause - not the throw keyword. This is used in method signatures in Java to indicate a checked exception can be thrown.

In C#, there is no direct equivalent of a Java checked exception. C# has no equivalent method signature clause.

// Java - need to have throws clause if IOException not handled
public void readFile() throws java.io.IOException {
  ...not explicitly handling java.io.IOException...
}

translates to

// C# - no equivalent of throws clause exceptions are unchecked
public void ReadFile() 
{
  ...not explicitly handling System.IO.IOException...
}
serg10