1

So I am fairly new to C#, and I've been learning by translating my code from python to C#. Now the problem I've stumbled upon, is: how do i catch the CS7036 error. It's called an "AttributeError" in python, and it happens, if you try to instantiate a class, without the required amount of arguments.

public Vector Crossproduct(Vector other)
{
    try
    {
        List<double> output = new List<double>()
        {
            Y* other.Z - other.Y * Z,0 - (X * other.Z- other.X * Z),X* other.Y - other.X * Y
        };
        Vector outputvector = new Vector(output);
        return outputvector;
    }
    catch (Exception)
    {

        throw;
    }
}

I've Googled this and found that there is almost nothing on this error. Here's a link to Microsoft's documentation for C#.Here and here.

My problem is not how to fix the error, but how to catch it, just so I'm clear.

Brian
  • 25,523
  • 18
  • 82
  • 173
  • 5
    Since that is not *possible* in C#, there's no need to catch it at runtime! – Eric Lippert Aug 23 '18 at 18:15
  • 3
    CS7036 isn't an exception thrown at runtime. It's a compiler error. That's why you can't catch it. –  Aug 23 '18 at 18:16
  • Try instantiating a class without providing it with the arguments and see what happens. The compiler will not allow it. – CodingYoshi Aug 23 '18 at 18:18
  • Why would it make sense for you to pass invalid arguments to the constructor? – Etienne de Martel Aug 23 '18 at 18:19
  • @EtiennedeMartel: Many things which are nonsensical are possible in Python! – Eric Lippert Aug 23 '18 at 18:20
  • @EricLippert I get why those things are runtime errors in Python because the type system is dynamic, but I don't understand how someone would try to use that as a feature... – Etienne de Martel Aug 23 '18 at 18:23
  • @EtiennedeMartel: I am not sure but perhaps if a constructor in Python has 3 arguments, creating it may make sense with one or two args as well. I guess that is how it could be a feature. In c#, it will be overloaded. – CodingYoshi Aug 23 '18 at 18:26
  • 1
    Thank you very much everyone! As I said, I am very new at C#. This will definitely help me a lot. – Atli H. Árnason Aug 23 '18 at 18:29
  • Writing `0 - (a - b)` is a strange way to write `b - a`. Can you say why you did that? – Eric Lippert Aug 23 '18 at 18:29
  • Do not feel that you have to edit the title of the question to say "solved" in the future. We know the problem is solved when you mark an answer as "accepted", as you've done. – Eric Lippert Aug 23 '18 at 22:55

2 Answers2

11

You are misunderstanding something fundamental here. Compiler errors are not runtime exceptions; they are the compiler telling you that the code is not legal at all. There is no facility for catching compiler errors at runtime because the illegal code will never run in the first place; it's illegal!

Now, there are situations in C# where a compiler error gets generated at runtime involving dynamic. That is: C# has a subsystem which allows it to interoperate with dynamic languages (such as Python or JavaScript). When using that feature, decisions which would normally be made at compile time are deferred until runtime, and in that situation, you can in fact be in scenario where you have to catch a compiler error at runtime.

You are not in that situation, and it is rare to be in a dynamic situation that involves a constructor. If you are in that situation in the future, the exception you want to catch is RuntimeBinderException.

See How does having a dynamic variable affect performance? for some thoughts on how dynamic works, if this subject interests you.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • Not all compilers refuse to compile illegal code. For example, the Eclipse Java compiler allows you to compile code that contains syntax errors. Mind you, using this feature in production code is an awful idea. Honestly, it feels weird that anyone would even use that feature during development. – Brian Aug 23 '18 at 21:24
  • 4
    @Brian: Back in the 1990s I worked on a JavaScript compiler that had a mode which would allow pretty much *any* program to compile, and would make a best-effort guess. As a demo, we would run *Visual Basic* programs through it, and some of the time, they kinda worked the same! But in my opinion, the design choice for JavaScript to be extremely forgiving of errors was a bad one; it meant that no two browsers implemented the same language, and thereby massively increased the testing burden of web devs. It would have been better to make JS extremely strict from the start. – Eric Lippert Aug 23 '18 at 22:53
0

C# is a compiled language, so you don't have to 'catch' this sort of error, as the compiler will catch it for you. Your code simply will not compile if you attempt to call a class constructor without the required parameters.