Craig Johnston
Craig Johnston

Reputation: 7607

C#: what happens if an exception is not caught

What happens in a C# program if an exception is not caught. Does the program 'crash' with something like a run-time error?

Upvotes: 1

Views: 5623

Answers (5)

Eric Lippert
Eric Lippert

Reputation: 660397

What happens in a C# program if an exception is not caught. Does the program 'crash' with something like a run-time error?

Maybe.

Maybe not.

What happens is implementation-defined.

For example, you might get a dialog box that says "there was an unhandled exception, and I see you have Visual Studio installed. Do you want to start up the debugger and examine the program state?"

You might get a dialog box that says "there was an unhandled exception, do you want to report this to Microsoft?"

If you are already running in the debugger, the debugger probably does something to bring it to your attention.

The runtime is allowed to do whatever it wants, and that includes asking you what to do.

Note that the runtime is aware of whether there's going to be a catch block or not before the finally blocks run. You can easily demonstrate this with a console app. Write an app that crashes, and outputs in the finally block:

Unhandled Exception: System.Exception: Exception of type 
'System.Exception' was thrown at 
ConsoleApplication1.Program.Main(String[] args) 
finally running now

See what happens? The runtime reports the error, gives the debugger a chance to run, or reports the problem to Microsoft, or whatever, before it runs the finally blocks. If they run at all. They might not. Anything can happen. The user could decide to destroy the process, or start a debugger and fix the exception, or whatever.

If you really want to understand how exceptions work in C# you should read this:

http://blogs.msdn.com/b/cbrumme/archive/2003/10/01/51524.aspx

Upvotes: 10

Justin Morgan
Justin Morgan

Reputation: 30700

Try it yourself!

class Program
{
    static void Main(string[] args)
    {
        int[] arr = new int[1];
        arr[5] = 6;   //throws an exception; what kind?
    }
}

Compile and run this in debug mode for a quick answer to your question.

Or just write some programs. Sooner or later, your code will throw exceptions; it happens to all of us, usually more times than we can count. In console or WinForms applications, an unhandled exception will usually crash the program; in ASP.NET, it will generate an error page, but it won't crash the whole website. You can also write custom code that specifies what to do in case of an unhandled exception, so that your application will fail gracefully.

Upvotes: 0

bnieland
bnieland

Reputation: 6506

Yes.

Something "exceptional" has happened, and your program does not know how to handle it, so it must stop execution at that point and "crash". There will be code that is executed after the crash, such as finally blocks, but basically the party is over for your code.

The best thing to do is to log these events, giving as much intofmation about the state of the system/program at the time of the crash. The Logging Application Block is one of the more robust automatic ways to log errors.

Upvotes: 1

stefan
stefan

Reputation: 2886

Yes.

Upvotes: 8

Dhaivat Pandya
Dhaivat Pandya

Reputation: 6536

Try it! Depending on the error, it will usually catch. Now, as for should all exceptions be caught, if its something like a[i] where it COULD throw an error if i is too big, but you knew that i was supposed to be kept within bounds (for example, in a for loop), you would not catch that exception.

However, if you are using data coming from a user (say, from a GUI) you would almost always validate it.

Upvotes: 0

Related Questions