Home > OS >  Visual Studio C# console build closes if an error occures, how can I let the console stay open?
Visual Studio C# console build closes if an error occures, how can I let the console stay open?

Time:03-10

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.

CodePudding user response:

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

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

CodePudding user response:

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
    {}
}
  • Related