0
public void division(int num1, int num2)
{
    try
    {
        result = num1 / num2;
    }
    catch (DivideByZeroException e)
    {
        Console.WriteLine("Exception caught: {0}", e);
    }
    finally
    {
        Console.WriteLine("Result: {0}", result);
    }
}

it's an interview question , how i prevent program from enter finally, return value directly without using exit(0) . i used to return directly after catch but it doesn't work

David L
  • 32,885
  • 8
  • 62
  • 93
  • 2
    you tryed 'return;'? –  Apr 18 '17 at 17:24
  • 2
    What's the point of having a `finally` if you want to skip it? – Arthur Rey Apr 18 '17 at 17:26
  • 1
    FailFast() may do it or p/invoking terminateprocess but in normal circumstances the answer is you can't. – Alex K. Apr 18 '17 at 17:27
  • yes,it doesn't work @anete.anetes – Aya Rashwan Apr 18 '17 at 17:28
  • You prevent it from entering the finally part by commenting it out. xD – David Lee Apr 18 '17 at 17:29
  • 2
    You cannot skip a `finally` block. The whole point of `finally` is that it's guaranteed to execute. – Kyle Apr 18 '17 at 17:30
  • @ArthurRey that's also my point of view ,but i put in situation to answer him – Aya Rashwan Apr 18 '17 at 17:31
  • 2
    Unplugging the computer at exactly the right moment should do the trick. – David Apr 18 '17 at 17:31
  • @AyaRashwan So what did you end up answering him? – I.B Apr 18 '17 at 17:34
  • The MSDN hints that certain unhandled exceptions can skip the `finally` block, but it depends on "how your computer is set up": [" However, if the exception is unhandled, execution of the finally block is dependent on how the exception unwind operation is triggered. That, in turn, is dependent on how your computer is set up. For more information, see Unhandled Exception Processing in the CLR."](https://msdn.microsoft.com/en-us/library/zwc8s4fz.aspx). Because you are only catching `DivideByZeroException`, throwing a different exception may work? – Quantic Apr 18 '17 at 17:35
  • 1
    @Quantic: Throwing a different exception won't make a difference, but an "unhandleable" exception may. A `StackOverflowException` for example is one where the physical resources available to the application have simply run out and it can't proceed to the next instruction, even if that next instruction is a `finally` block. – David Apr 18 '17 at 17:37
  • @CNuts i answered " what's the core of adding finally without using it? i told him to remove it" btw i rejected :D – Aya Rashwan Apr 18 '17 at 17:53
  • @Rashwan haha that's good, he didn't tell you what he thought was the correct answer? – I.B Apr 18 '17 at 18:15
  • 1
    @CNuts i asked but he answered " google it" :D – Aya Rashwan Apr 18 '17 at 18:48

2 Answers2

1

Remove the finally block and put the second call to WriteLine in the try block:

public void division(int num1, int num2)
{
    try
    {
        result = num1 / num2;
        Console.WriteLine("Result: {0}", result);
    }
    catch (DivideByZeroException e)
    {
        Console.WriteLine("Exception caught: {0}", e);
    }
}

It makes no sense to define a finally block if you don't want to execute the code in it...

mm8
  • 163,881
  • 10
  • 57
  • 88
  • 4
    Somehow I feel like this is missing the spirit of the interview question.... – David L Apr 18 '17 at 17:29
  • This is, or at least should be, the only acceptable answer to the interview question. As mentioned in another comment, the whole point of using a finally block in the first place is that it's guaranteed to execute. If you don't want this you should remove it. – mm8 Apr 18 '17 at 17:30
0

The block always executes thats why it created. You can try to control it with a boolean variable.

DigheadsFerke
  • 307
  • 1
  • 8