1

Console application (build) closes if an error occures, how can I let the console stay open after the error occures? (For debbuging reasons, and yes the application has to be build for my purposes)

Console.ReadKey() is not what I am looking for, just wondering if I can prevent the exit on error.

Erik
  • 11
  • 2

2 Answers2

0

You can use [try, catch, finally] operator to block errors

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch-finally

  • That is about catching exceptions, which the asker might very well be able to. It doesn't solve keeping the console open. – Palle Due Mar 10 '22 at 13:16
-2

The piece of code which can casue exception should be written inside

try{} catch{}

Example: In this for loop first iteration should give an Index out bound error but still it continues to print ignoring the exception case. In catch you can do whatever you want to do with the exception.

int v = 100;
int[] a = new int[v];
for (int i = 0; i < v; i++)`
{
    try
    {
        Console.WriteLine(a[i - 1].ToString() + " - Count: " + i);
    }
    catch
    {}
}
  • 1
    That is about catching exceptions, which the asker might very well be able to. It doesn't solve keeping the console open. – Palle Due Mar 10 '22 at 13:16
  • @PalleDue Yes, I'm very well aware that concern of the question is to keep the console open. But the context of it is already given that the questioner wants to prevent the closing of console at error. I think my answer pretty well serve the solution for the question asked. – Ansari Aquib Mar 10 '22 at 18:55