简体   繁体   中英

Unhandled exception in C# application does it crash the app?

In my C# application when I run it in debug mode in Visual Studio, it breaks saying I have an unhandled exception (it is a System.ArgumentException). But when I run it in release mode, the application does not crash.

My question is what is the consequence if I have unhandled exception? Does ithe application crash when user runs into the scenario?

Unhandled exception in C# application does it crash the app?

If you mean 'unhandled' in a sense 'unhandled by me' then it's not necessarily. Lets say that your method starts from static void Main (in main thread) and it throws exception that is not handled. This should crash your application. But on the other hand if you run the same method asynchronously in a new Task(Method) it'll crash the Thread where this Task is executed yes, but not your application (this exception will be handled automatically by the Task Manager).

PS So any exception that is unhandled terminates program flow.

If the exception is really unhandled (it bubbles through all layers) than it´ll result in a crash of your app. However as others already mentioned before when the exception is just thrown on some line of code and catched an any other decent point VS may show the error and afterwards will continue debugging within your catch-handler.

Consider this code:

void Main(string[] args) 
{
    try {
        DoSomething();
    } catch {
        /* error-handling happens here */
    }
}

void DoSomething {
    // this exception will bubble until Main and is handled there --> no crash
    throw new ArgumentException();
}

However if you do not have any try-catch -block within your Main -method (or within DoSomething ) the application will also bubble through but hence it is not handled it will lead to app-crash.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM