I wonder if at least according to ECMA-334, section 15.9.5, the behaviour of following is undefined:

using System;

class FindingCountry
{
    public static void Main(String[] args)
    {
    int a=0;
    try
    {
        throw new ArgumentNullException((5 / a).ToString());
    }
    catch (DivideByZeroException)
    {
        
    }
    
    }

     
}

Running it with the VS 2008 C# compiler, the exception that's thrown in the code actually never gets propagated...

Recommended Answers

All 5 Replies

>Undefined Exception Behaviour?

Undefined behavior either not allowed or throw an exception.

Running it with the VS 2008 C# compiler, the exception that's thrown in the code actually never gets propagated...

Your code is doing exactly what you have told it to do. During the creation of the ArgumentNullException, you created a DivideByZeroException.

1) The creation of the ArgumentNullException did not succeed and, in fact, the DivideByZeroException occurred before the constructor to ArgumentNullException was even called. (The calculation in the argument would be evaluated first.)
2) A DivideByZeroException was thrown.
3) You caught but did not provide a behavior to follow for the exception.
4) Your program was allowed to exit normally.

Have you checked the output window in VS? Depending on the options you have selected in VS, some exceptions dont break to debug...look for "a first chance exception of type 'System.DivideByZeroException' occured in ..." in the output window.

jk451, can you clarify what behaviour you were expecting?
As i pointed out, if you arent seeing the divide by zero exception it is possibly being shown in your output. If you step throught with the debugger you will see that the catch method is executed (but its empty).

If you were expecting the ArgumentNullException to throw, it wont because (as apegram pointed out) the paramter is parsed first, wherein an error is thrown preventing the rest of the try block from executing. So the ArgumentNullException is never created or thrown. Thats the intended behaviour as far as i can see. As soon as an error is encountered the code excecution ceases and the nearest enclosing catch method is run.

jk451, can you clarify what behaviour you were expecting?

Well, I didn't actually know, but I agree that the behaviour is actually OK, since it's true, as apegram said, that thanks to call-by-value, the argument will get evaluated first and it's then completely OK that control passes into the (empty) catch block without returning to the throw.

Thanks for clarifying this.

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.