Can you cancel the stackoverflow exception in the following code?

2

The following code will produce a run-time error, stackoverflow exception.

class Foo
{
    //static Foo foo = new Foo(); // you are not allowed using this approach
    //static readonly Foo foo = new Foo(); // you are not allowed using this approach
    Foo foo = new Foo();
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
    }
}

The objective is to cancel the exception without

  • removing, modifying or commenting out the existing code,
  • using the approaches mentioned (and their variants in access modifiers, public, private, etc) as the given comments,
  • using try-catch-finally or its variants,
  • using attributes,
  • using compilation condition,

You are only allowed to add your own code inside the Foo class. How do you cancel the exception at run-time?

kiss my armpit

Posted 2014-01-06T01:29:24.777

Reputation: 325

Question was closed 2014-12-01T16:53:12.053

It would be best to tag this question as either [tag:popularity-contest] or [tag:code-golf] so it's clear how you will objectively declare a winner. – Darren Stone – 2014-01-06T01:51:45.580

I probably will add more rules if your ideas are too trivial. Sorry for this inconvenience. – kiss my armpit – 2014-01-06T02:02:56.553

5Pulling the rug out like that may be considered rude. It's best to think your question through thoroughly before asking the internet to spend effort on it. :-) – Darren Stone – 2014-01-06T02:10:37.430

1This is a [tag:popularity-contest] yet the accepted answer has few votes. You should accept the highest-voted answer (see [tag:popularity-contest]) – Justin – 2014-01-10T03:43:14.093

1@Quincunx: I changed the tag. – kiss my armpit – 2014-01-10T03:58:49.283

You need winning criterion (?) – Timtech – 2014-12-01T13:20:16.907

Answers

9

Never wrote C# before, does this work ?

class Foo
{
    Foo fooExit = exitMe();
    //static Foo foo = new Foo(); // you are not allowed using this approach
    //static readonly Foo foo = new Foo(); // you are not allowed using this approach
    Foo foo = new Foo();

    static Foo exitMe()
    {
        System.Environment.Exit(0);
        return null;
    }
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
    }
}

Florent Bayle

Posted 2014-01-06T01:29:24.777

Reputation: 647

Well, the rules forbid "using attributes". – Victor Stafusa – 2014-01-06T02:24:20.483

@Victor: The code above does not contain any attributes. – kiss my armpit – 2014-01-06T02:25:27.640

@StiffJokes Interesting. I am from Java and never programmed in C#. In Java the Foo fooExit = exitMe(); would be a declaration of an attribute called fooExit. I.E. For me attribute and field were synonyms. Googling this, it looks like that in C# attributes are more-or-less what in java are called annotations. Thank you, did not knew this! – Victor Stafusa – 2014-01-06T02:32:24.540

I changed the tag just for attracting more creative method. :-) – kiss my armpit – 2014-01-06T02:36:39.207

The winner might be different from the accepted answer. It does not matter for me. – kiss my armpit – 2014-01-06T13:06:30.187

foo must be declared after fooExit to make the trick work. – kiss my armpit – 2014-01-06T13:10:14.860

As I see it, this does not cancel the exception but exit the program, before it is thrown. Does that really count? – Oliver Friedrich – 2014-01-13T13:37:53.927

31

Here, another try:

class Foo
{
    //static Foo foo = new Foo(); // you are not allowed using this approach
    //static readonly Foo foo = new Foo(); // you are not allowed using this approach
    Foo foo = new Foo();
    MWAHAHAHA, THIS LINE GIVES A COMPILE ERROR! NO STACKOVERFLOW EXCEPTION ANYMORE! LOL
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
    }
}

Victor Stafusa

Posted 2014-01-06T01:29:24.777

Reputation: 8 612

1It is really funny, really. – kiss my armpit – 2014-01-06T02:20:57.463

3That's what I was gonna do... – boothby – 2014-01-06T02:43:02.700

25

How about this?

class Foo
{
    //static Foo foo = new Foo(); // you are not allowed using this approach
    //static readonly Foo foo = new Foo(); // you are not allowed using this approach
    String str = @"
    Foo foo = new Foo();
    ";
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
    }
}

simon

Posted 2014-01-06T01:29:24.777

Reputation: 393

14

class Foo
{
    public class Bar
    {
        Foo foo = new Foo();
    }
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
    }
}

I don't have a C# compiler at hand, but I suspect this might work. The idea is to put the definition in a nested class, such that foo would only be assigned if an instance of Bar is created. Now, the problem is that (I believe) the nested class needs to be public. I don't know if this is a rule violation, i.e. that the rule applies to any use of an access modifier, or only if applied to foo. (Could someone who can compile C# please try this with and without the public?)

nitro2k01

Posted 2014-01-06T01:29:24.777

Reputation: 1 283

2Works with and without public. You also could've tested this on an online compiler, e.g. ideone. – Bob – 2014-01-06T06:04:39.340

8

class Foo
{
    Foo(bool recursion = false)
    {
        if (recursion)
            Foo foo = new Foo();
    }
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
    }
}

Hand-E-Food

Posted 2014-01-06T01:29:24.777

Reputation: 7 912

8

To simplify Florent's code:

class Foo
{
    Foo foo = new Foo();

    static Foo() {
        System.Environment.Exit(1);
    }

}

Andris

Posted 2014-01-06T01:29:24.777

Reputation: 406

Does this actually work? Won't Foo foo = new Foo(); be (recursively) evaluated before any constructor is executed? – nitro2k01 – 2014-02-18T09:35:13.780

@nitro2k01 No, it won't. The static ctor is executed before any instance member initialization. – Andris – 2014-02-18T10:07:37.560

3

class Foo
{
    while (true);
    Foo foo = new Foo();
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
    }
}

Its not a bug, it's a feature!

In case that doesn't work

class Foo
    if(false)
        Foo foo = new Foo();
}

class Program
{

    static void Main(string[] args)
    {
        new Foo();
    }
}

Mark

Posted 2014-01-06T01:29:24.777

Reputation: 179

1I guess that stopping stack overflow from happening by making infinite loop prior to it counts. It doesn't appear to be disallowed. – Konrad Borowski – 2014-01-10T13:27:50.630

@xfix Indeed. Since the problematic code will never be executed, it cannot cause a lockup. Aka: You cannot lock me up because I'll lock me up first :-P – Mark – 2014-01-10T13:30:14.447

2I can't see how that would compile in C#. I'm getting a Invalid token 'while' in class, struct, or interface member declaration compiler error with your code. – Andris – 2014-01-10T13:39:04.050

1@Mark - Hmmm, seems a little passive-aggressive... ;) I like it. – simon – 2014-01-13T22:38:20.903

@simon It's called c-SHARP, because its not for the soft-hearted :-P – Mark – 2014-01-14T06:39:33.697

3

The Answer is:

class Foo
{
    //static Foo foo = new Foo(); // you are not allowed using this approach
    //static readonly Foo foo = new Foo(); // you are not allowed using this approach

    public void Method()
    {
    Foo foo = new Foo();
    }
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
        Console.ReadLine();
    }
}

satarupa

Posted 2014-01-06T01:29:24.777

Reputation: 41

1

Note: The rules changed since I posted this.

That is it:

class Foo
{
    //static Foo foo = new Foo(); // you are not allowed using this approach
    //static readonly Foo foo = new Foo(); // you are not allowed using this approach
    Foo foo = null; // Removing code != modifying.
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
    }
}

Victor Stafusa

Posted 2014-01-06T01:29:24.777

Reputation: 8 612

It is almost funny, just almost. :-) – kiss my armpit – 2014-01-06T02:13:35.867

Your trivial code can actually be simplified as Foo foo; to make it almost funnier. – kiss my armpit – 2014-01-06T02:22:14.280

@StiffJokes In this case, you could argue that I was deleting code, against the rules (as they were at that time). – Victor Stafusa – 2014-01-06T02:23:16.370

0

class Foo
{
    //static Foo foo = new Foo(); // you are not allowed using this approach
    //static readonly Foo foo = new Foo(); // you are not allowed using this approach
    Foo foo = true?null: new Foo();
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
    }
}

ilyennevnincs

Posted 2014-01-06T01:29:24.777

Reputation: 1