finally not called after try












2















For some reason within my console application I cannot get my finally block to run. I was writing this code to test how the finally block works so it is very simple:



static void Main()
{
int i = 0;
try
{
int j = 1 / i; // Generate a divide by 0 exception.
}
finally
{

Console.Out.WriteLine("Finished");
Console.In.ReadLine();
}
}


At first I had the problem described here but then I tried to run the program outside Visual Studio I got a "Program has stopped responding" error.










share|improve this question

























  • Hm, I'm not quite familar with C#, but you aren't catching the division by zero exception at all...maybe this is the error?

    – akluth
    Aug 28 '12 at 16:00











  • Try taking out the ReadLine and run it in a command prompt outside Visual Studio. What happens then ?

    – user957902
    Aug 28 '12 at 16:04











  • I've tried that. It acts the same.

    – Fr33dan
    Aug 28 '12 at 16:05






  • 2





    See this: stackoverflow.com/questions/3421738/…

    – vpascoal
    Aug 28 '12 at 16:06






  • 1





    Works fine for me - I get the unhandled exception error box, I select 'Close the program' and then the code in the finally block runs - display Finished and wait for ENTER to be pressed.

    – MiMo
    Aug 28 '12 at 16:07
















2















For some reason within my console application I cannot get my finally block to run. I was writing this code to test how the finally block works so it is very simple:



static void Main()
{
int i = 0;
try
{
int j = 1 / i; // Generate a divide by 0 exception.
}
finally
{

Console.Out.WriteLine("Finished");
Console.In.ReadLine();
}
}


At first I had the problem described here but then I tried to run the program outside Visual Studio I got a "Program has stopped responding" error.










share|improve this question

























  • Hm, I'm not quite familar with C#, but you aren't catching the division by zero exception at all...maybe this is the error?

    – akluth
    Aug 28 '12 at 16:00











  • Try taking out the ReadLine and run it in a command prompt outside Visual Studio. What happens then ?

    – user957902
    Aug 28 '12 at 16:04











  • I've tried that. It acts the same.

    – Fr33dan
    Aug 28 '12 at 16:05






  • 2





    See this: stackoverflow.com/questions/3421738/…

    – vpascoal
    Aug 28 '12 at 16:06






  • 1





    Works fine for me - I get the unhandled exception error box, I select 'Close the program' and then the code in the finally block runs - display Finished and wait for ENTER to be pressed.

    – MiMo
    Aug 28 '12 at 16:07














2












2








2


0






For some reason within my console application I cannot get my finally block to run. I was writing this code to test how the finally block works so it is very simple:



static void Main()
{
int i = 0;
try
{
int j = 1 / i; // Generate a divide by 0 exception.
}
finally
{

Console.Out.WriteLine("Finished");
Console.In.ReadLine();
}
}


At first I had the problem described here but then I tried to run the program outside Visual Studio I got a "Program has stopped responding" error.










share|improve this question
















For some reason within my console application I cannot get my finally block to run. I was writing this code to test how the finally block works so it is very simple:



static void Main()
{
int i = 0;
try
{
int j = 1 / i; // Generate a divide by 0 exception.
}
finally
{

Console.Out.WriteLine("Finished");
Console.In.ReadLine();
}
}


At first I had the problem described here but then I tried to run the program outside Visual Studio I got a "Program has stopped responding" error.







c# exception-handling try-finally






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 5 '17 at 20:01









Mark Amery

63.6k31253303




63.6k31253303










asked Aug 28 '12 at 15:59









Fr33danFr33dan

2,75922952




2,75922952













  • Hm, I'm not quite familar with C#, but you aren't catching the division by zero exception at all...maybe this is the error?

    – akluth
    Aug 28 '12 at 16:00











  • Try taking out the ReadLine and run it in a command prompt outside Visual Studio. What happens then ?

    – user957902
    Aug 28 '12 at 16:04











  • I've tried that. It acts the same.

    – Fr33dan
    Aug 28 '12 at 16:05






  • 2





    See this: stackoverflow.com/questions/3421738/…

    – vpascoal
    Aug 28 '12 at 16:06






  • 1





    Works fine for me - I get the unhandled exception error box, I select 'Close the program' and then the code in the finally block runs - display Finished and wait for ENTER to be pressed.

    – MiMo
    Aug 28 '12 at 16:07



















  • Hm, I'm not quite familar with C#, but you aren't catching the division by zero exception at all...maybe this is the error?

    – akluth
    Aug 28 '12 at 16:00











  • Try taking out the ReadLine and run it in a command prompt outside Visual Studio. What happens then ?

    – user957902
    Aug 28 '12 at 16:04











  • I've tried that. It acts the same.

    – Fr33dan
    Aug 28 '12 at 16:05






  • 2





    See this: stackoverflow.com/questions/3421738/…

    – vpascoal
    Aug 28 '12 at 16:06






  • 1





    Works fine for me - I get the unhandled exception error box, I select 'Close the program' and then the code in the finally block runs - display Finished and wait for ENTER to be pressed.

    – MiMo
    Aug 28 '12 at 16:07

















Hm, I'm not quite familar with C#, but you aren't catching the division by zero exception at all...maybe this is the error?

– akluth
Aug 28 '12 at 16:00





Hm, I'm not quite familar with C#, but you aren't catching the division by zero exception at all...maybe this is the error?

– akluth
Aug 28 '12 at 16:00













Try taking out the ReadLine and run it in a command prompt outside Visual Studio. What happens then ?

– user957902
Aug 28 '12 at 16:04





Try taking out the ReadLine and run it in a command prompt outside Visual Studio. What happens then ?

– user957902
Aug 28 '12 at 16:04













I've tried that. It acts the same.

– Fr33dan
Aug 28 '12 at 16:05





I've tried that. It acts the same.

– Fr33dan
Aug 28 '12 at 16:05




2




2





See this: stackoverflow.com/questions/3421738/…

– vpascoal
Aug 28 '12 at 16:06





See this: stackoverflow.com/questions/3421738/…

– vpascoal
Aug 28 '12 at 16:06




1




1





Works fine for me - I get the unhandled exception error box, I select 'Close the program' and then the code in the finally block runs - display Finished and wait for ENTER to be pressed.

– MiMo
Aug 28 '12 at 16:07





Works fine for me - I get the unhandled exception error box, I select 'Close the program' and then the code in the finally block runs - display Finished and wait for ENTER to be pressed.

– MiMo
Aug 28 '12 at 16:07












4 Answers
4






active

oldest

votes


















11














Because you do not have a top level exception handler, the .Net runtime is catching the exception for you and aborting the program before the finally has a chance to run. This illustrates the point:



static void Main() 
{
try
{
int i = 0;
try
{
int j = 1 / i; // Generate a divide by 0 exception.
}
finally
{
Console.Out.WriteLine("Finished");
Console.In.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}


With this code, the exception is now handled a try...catch higher up in the calling chain (it just happens to be in the same method), so the embedded finally will be executed. The catch need not be in the same function where the exception is being raised, it can be anywhere in the calling chain.



Edit: initially it may seem uncertain when and where an exception will be caught by your program. But think about the boundaries of your application, where the outside world interacts with your code - normally they are limited and well defined. So for a console application, the boundary is the Main method, and this is where you can put the top level exception handler. For a Web forms application, the boundary includes things like button click events (the user is interacting with your UI), so you could have exception handlers in there too. For a class library the boundary is generally the boundary of the application that calls the library, not the library itself. So do not catch exceptions in the library (unless you can sensibly recover from them), but let them bubble up to the calling application instead.






share|improve this answer





















  • 1





    +1 Thank you for explaining that you do need a catch block, but unlike other answers, outside of the scope of the try-finally block in question.

    – Steve Guidi
    Aug 28 '12 at 16:13











  • I understand, the exception is passed to the higher level before the finally is called. Since there is no higher level the program halts and quits before the finally. But does this mean I can never be sure that my finally will run since I may not know the status of the calling chain?

    – Fr33dan
    Aug 28 '12 at 16:29











  • No. Finallys will always run. The only case they don't is when you have a major problem with your code (unhandled exception which is not caught anywhere). The runtime does not know what to do with the exception so the code cannot proceed...

    – MoonKnight
    Aug 28 '12 at 16:33











  • Right so if I'm writing a single method I cannot be sure exceptions thrown within the try will eventually be caught by something. So in such a context I cannot guarantee my finally will be called (unless I catch the exceptions myself)

    – Fr33dan
    Aug 28 '12 at 16:45











  • Hmm. Some docs explaining precisely what happens, in what order, when there's an uncaught exception in Main() would improve this answer - the behavior shown here is frankly pretty weird and unlike any other language I've used. I can't find any such docs, though.

    – Mark Amery
    Nov 5 '17 at 20:32



















1














In a larger program this would not be a problem as the DevideByZero exception would "bubble-up" and would hopefully be dealt with somewhere else. Because this is in the main method, the exception has no where to go. This causes the problem you see...



So the following would do as you expect



static void Main(string args)
{
try
{
CatchTest();
}
catch (Exception)
{

}
}

private static void CatchTest()
{
int i = 0;
try
{
int j = 1 / i; // Generate a divide by 0 exception.
}
finally
{
Console.Out.WriteLine("Finished");
Console.In.ReadLine();
}
}


I hope this helps.






share|improve this answer





















  • 2





    Downvoter care to comment?

    – MoonKnight
    Aug 28 '12 at 16:17






  • 1





    +1. This answer is OK to me.

    – Polyfun
    Aug 28 '12 at 16:30



















0














You still need a catch to capture the exception throwing:



int i = 0;
try
{
int j = 1 / i; // Generate a divide by 0 exception.
}
catch(Exception e)
{
Console.Out.WriteLine("Exception caught");
}
finally
{

Console.Out.WriteLine("Finished");
Console.In.ReadLine();
}


This exception is causing the application to crash, and the exception is unhandled therefore terminating the application.



http://msdn.microsoft.com/en-us/library/zwc8s4fz(v=vs.100).aspx




Usually, when an unhandled exception ends an application, whether or not the finally block is run is not important. However, if you have statements in a finally block that must be run even in that situation, one solution is to add a catch block to the try-finally statement.







share|improve this answer


























  • No because I'm trying to use try-finally: msdn.microsoft.com/en-us/library/zwc8s4fz%28v=vs.71%29.aspx

    – Fr33dan
    Aug 28 '12 at 16:01











  • Again, when there's an error you never get to the finally.

    – carny666
    Aug 28 '12 at 16:07











  • No. This is not right. The finally block will always get called as long as the exception has somewhere to go. In this case it doesn't...

    – MoonKnight
    Aug 28 '12 at 16:11











  • @carny666 it's happening because he's running it in a debugger, if he runs it in release mode the finally will be executed. See this question for more details: stackoverflow.com/questions/3421738/… application-while-using-f5

    – Kiril
    Aug 28 '12 at 16:11











  • @Lirik yup.. I get that now.

    – carny666
    Aug 28 '12 at 16:12



















0














You need to detach the debugger (e.g. run your application in Release mode), see the following (related) question for more details: finally doesn't seem to execute in C# console application while using F5






share|improve this answer


























  • I actually mention this issue and reference that question in my question. I've tried running it outside the debugger.

    – Fr33dan
    Aug 28 '12 at 16:25











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12163149%2ffinally-not-called-after-try%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









11














Because you do not have a top level exception handler, the .Net runtime is catching the exception for you and aborting the program before the finally has a chance to run. This illustrates the point:



static void Main() 
{
try
{
int i = 0;
try
{
int j = 1 / i; // Generate a divide by 0 exception.
}
finally
{
Console.Out.WriteLine("Finished");
Console.In.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}


With this code, the exception is now handled a try...catch higher up in the calling chain (it just happens to be in the same method), so the embedded finally will be executed. The catch need not be in the same function where the exception is being raised, it can be anywhere in the calling chain.



Edit: initially it may seem uncertain when and where an exception will be caught by your program. But think about the boundaries of your application, where the outside world interacts with your code - normally they are limited and well defined. So for a console application, the boundary is the Main method, and this is where you can put the top level exception handler. For a Web forms application, the boundary includes things like button click events (the user is interacting with your UI), so you could have exception handlers in there too. For a class library the boundary is generally the boundary of the application that calls the library, not the library itself. So do not catch exceptions in the library (unless you can sensibly recover from them), but let them bubble up to the calling application instead.






share|improve this answer





















  • 1





    +1 Thank you for explaining that you do need a catch block, but unlike other answers, outside of the scope of the try-finally block in question.

    – Steve Guidi
    Aug 28 '12 at 16:13











  • I understand, the exception is passed to the higher level before the finally is called. Since there is no higher level the program halts and quits before the finally. But does this mean I can never be sure that my finally will run since I may not know the status of the calling chain?

    – Fr33dan
    Aug 28 '12 at 16:29











  • No. Finallys will always run. The only case they don't is when you have a major problem with your code (unhandled exception which is not caught anywhere). The runtime does not know what to do with the exception so the code cannot proceed...

    – MoonKnight
    Aug 28 '12 at 16:33











  • Right so if I'm writing a single method I cannot be sure exceptions thrown within the try will eventually be caught by something. So in such a context I cannot guarantee my finally will be called (unless I catch the exceptions myself)

    – Fr33dan
    Aug 28 '12 at 16:45











  • Hmm. Some docs explaining precisely what happens, in what order, when there's an uncaught exception in Main() would improve this answer - the behavior shown here is frankly pretty weird and unlike any other language I've used. I can't find any such docs, though.

    – Mark Amery
    Nov 5 '17 at 20:32
















11














Because you do not have a top level exception handler, the .Net runtime is catching the exception for you and aborting the program before the finally has a chance to run. This illustrates the point:



static void Main() 
{
try
{
int i = 0;
try
{
int j = 1 / i; // Generate a divide by 0 exception.
}
finally
{
Console.Out.WriteLine("Finished");
Console.In.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}


With this code, the exception is now handled a try...catch higher up in the calling chain (it just happens to be in the same method), so the embedded finally will be executed. The catch need not be in the same function where the exception is being raised, it can be anywhere in the calling chain.



Edit: initially it may seem uncertain when and where an exception will be caught by your program. But think about the boundaries of your application, where the outside world interacts with your code - normally they are limited and well defined. So for a console application, the boundary is the Main method, and this is where you can put the top level exception handler. For a Web forms application, the boundary includes things like button click events (the user is interacting with your UI), so you could have exception handlers in there too. For a class library the boundary is generally the boundary of the application that calls the library, not the library itself. So do not catch exceptions in the library (unless you can sensibly recover from them), but let them bubble up to the calling application instead.






share|improve this answer





















  • 1





    +1 Thank you for explaining that you do need a catch block, but unlike other answers, outside of the scope of the try-finally block in question.

    – Steve Guidi
    Aug 28 '12 at 16:13











  • I understand, the exception is passed to the higher level before the finally is called. Since there is no higher level the program halts and quits before the finally. But does this mean I can never be sure that my finally will run since I may not know the status of the calling chain?

    – Fr33dan
    Aug 28 '12 at 16:29











  • No. Finallys will always run. The only case they don't is when you have a major problem with your code (unhandled exception which is not caught anywhere). The runtime does not know what to do with the exception so the code cannot proceed...

    – MoonKnight
    Aug 28 '12 at 16:33











  • Right so if I'm writing a single method I cannot be sure exceptions thrown within the try will eventually be caught by something. So in such a context I cannot guarantee my finally will be called (unless I catch the exceptions myself)

    – Fr33dan
    Aug 28 '12 at 16:45











  • Hmm. Some docs explaining precisely what happens, in what order, when there's an uncaught exception in Main() would improve this answer - the behavior shown here is frankly pretty weird and unlike any other language I've used. I can't find any such docs, though.

    – Mark Amery
    Nov 5 '17 at 20:32














11












11








11







Because you do not have a top level exception handler, the .Net runtime is catching the exception for you and aborting the program before the finally has a chance to run. This illustrates the point:



static void Main() 
{
try
{
int i = 0;
try
{
int j = 1 / i; // Generate a divide by 0 exception.
}
finally
{
Console.Out.WriteLine("Finished");
Console.In.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}


With this code, the exception is now handled a try...catch higher up in the calling chain (it just happens to be in the same method), so the embedded finally will be executed. The catch need not be in the same function where the exception is being raised, it can be anywhere in the calling chain.



Edit: initially it may seem uncertain when and where an exception will be caught by your program. But think about the boundaries of your application, where the outside world interacts with your code - normally they are limited and well defined. So for a console application, the boundary is the Main method, and this is where you can put the top level exception handler. For a Web forms application, the boundary includes things like button click events (the user is interacting with your UI), so you could have exception handlers in there too. For a class library the boundary is generally the boundary of the application that calls the library, not the library itself. So do not catch exceptions in the library (unless you can sensibly recover from them), but let them bubble up to the calling application instead.






share|improve this answer















Because you do not have a top level exception handler, the .Net runtime is catching the exception for you and aborting the program before the finally has a chance to run. This illustrates the point:



static void Main() 
{
try
{
int i = 0;
try
{
int j = 1 / i; // Generate a divide by 0 exception.
}
finally
{
Console.Out.WriteLine("Finished");
Console.In.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}


With this code, the exception is now handled a try...catch higher up in the calling chain (it just happens to be in the same method), so the embedded finally will be executed. The catch need not be in the same function where the exception is being raised, it can be anywhere in the calling chain.



Edit: initially it may seem uncertain when and where an exception will be caught by your program. But think about the boundaries of your application, where the outside world interacts with your code - normally they are limited and well defined. So for a console application, the boundary is the Main method, and this is where you can put the top level exception handler. For a Web forms application, the boundary includes things like button click events (the user is interacting with your UI), so you could have exception handlers in there too. For a class library the boundary is generally the boundary of the application that calls the library, not the library itself. So do not catch exceptions in the library (unless you can sensibly recover from them), but let them bubble up to the calling application instead.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 14:40

























answered Aug 28 '12 at 16:09









PolyfunPolyfun

7,68242736




7,68242736








  • 1





    +1 Thank you for explaining that you do need a catch block, but unlike other answers, outside of the scope of the try-finally block in question.

    – Steve Guidi
    Aug 28 '12 at 16:13











  • I understand, the exception is passed to the higher level before the finally is called. Since there is no higher level the program halts and quits before the finally. But does this mean I can never be sure that my finally will run since I may not know the status of the calling chain?

    – Fr33dan
    Aug 28 '12 at 16:29











  • No. Finallys will always run. The only case they don't is when you have a major problem with your code (unhandled exception which is not caught anywhere). The runtime does not know what to do with the exception so the code cannot proceed...

    – MoonKnight
    Aug 28 '12 at 16:33











  • Right so if I'm writing a single method I cannot be sure exceptions thrown within the try will eventually be caught by something. So in such a context I cannot guarantee my finally will be called (unless I catch the exceptions myself)

    – Fr33dan
    Aug 28 '12 at 16:45











  • Hmm. Some docs explaining precisely what happens, in what order, when there's an uncaught exception in Main() would improve this answer - the behavior shown here is frankly pretty weird and unlike any other language I've used. I can't find any such docs, though.

    – Mark Amery
    Nov 5 '17 at 20:32














  • 1





    +1 Thank you for explaining that you do need a catch block, but unlike other answers, outside of the scope of the try-finally block in question.

    – Steve Guidi
    Aug 28 '12 at 16:13











  • I understand, the exception is passed to the higher level before the finally is called. Since there is no higher level the program halts and quits before the finally. But does this mean I can never be sure that my finally will run since I may not know the status of the calling chain?

    – Fr33dan
    Aug 28 '12 at 16:29











  • No. Finallys will always run. The only case they don't is when you have a major problem with your code (unhandled exception which is not caught anywhere). The runtime does not know what to do with the exception so the code cannot proceed...

    – MoonKnight
    Aug 28 '12 at 16:33











  • Right so if I'm writing a single method I cannot be sure exceptions thrown within the try will eventually be caught by something. So in such a context I cannot guarantee my finally will be called (unless I catch the exceptions myself)

    – Fr33dan
    Aug 28 '12 at 16:45











  • Hmm. Some docs explaining precisely what happens, in what order, when there's an uncaught exception in Main() would improve this answer - the behavior shown here is frankly pretty weird and unlike any other language I've used. I can't find any such docs, though.

    – Mark Amery
    Nov 5 '17 at 20:32








1




1





+1 Thank you for explaining that you do need a catch block, but unlike other answers, outside of the scope of the try-finally block in question.

– Steve Guidi
Aug 28 '12 at 16:13





+1 Thank you for explaining that you do need a catch block, but unlike other answers, outside of the scope of the try-finally block in question.

– Steve Guidi
Aug 28 '12 at 16:13













I understand, the exception is passed to the higher level before the finally is called. Since there is no higher level the program halts and quits before the finally. But does this mean I can never be sure that my finally will run since I may not know the status of the calling chain?

– Fr33dan
Aug 28 '12 at 16:29





I understand, the exception is passed to the higher level before the finally is called. Since there is no higher level the program halts and quits before the finally. But does this mean I can never be sure that my finally will run since I may not know the status of the calling chain?

– Fr33dan
Aug 28 '12 at 16:29













No. Finallys will always run. The only case they don't is when you have a major problem with your code (unhandled exception which is not caught anywhere). The runtime does not know what to do with the exception so the code cannot proceed...

– MoonKnight
Aug 28 '12 at 16:33





No. Finallys will always run. The only case they don't is when you have a major problem with your code (unhandled exception which is not caught anywhere). The runtime does not know what to do with the exception so the code cannot proceed...

– MoonKnight
Aug 28 '12 at 16:33













Right so if I'm writing a single method I cannot be sure exceptions thrown within the try will eventually be caught by something. So in such a context I cannot guarantee my finally will be called (unless I catch the exceptions myself)

– Fr33dan
Aug 28 '12 at 16:45





Right so if I'm writing a single method I cannot be sure exceptions thrown within the try will eventually be caught by something. So in such a context I cannot guarantee my finally will be called (unless I catch the exceptions myself)

– Fr33dan
Aug 28 '12 at 16:45













Hmm. Some docs explaining precisely what happens, in what order, when there's an uncaught exception in Main() would improve this answer - the behavior shown here is frankly pretty weird and unlike any other language I've used. I can't find any such docs, though.

– Mark Amery
Nov 5 '17 at 20:32





Hmm. Some docs explaining precisely what happens, in what order, when there's an uncaught exception in Main() would improve this answer - the behavior shown here is frankly pretty weird and unlike any other language I've used. I can't find any such docs, though.

– Mark Amery
Nov 5 '17 at 20:32













1














In a larger program this would not be a problem as the DevideByZero exception would "bubble-up" and would hopefully be dealt with somewhere else. Because this is in the main method, the exception has no where to go. This causes the problem you see...



So the following would do as you expect



static void Main(string args)
{
try
{
CatchTest();
}
catch (Exception)
{

}
}

private static void CatchTest()
{
int i = 0;
try
{
int j = 1 / i; // Generate a divide by 0 exception.
}
finally
{
Console.Out.WriteLine("Finished");
Console.In.ReadLine();
}
}


I hope this helps.






share|improve this answer





















  • 2





    Downvoter care to comment?

    – MoonKnight
    Aug 28 '12 at 16:17






  • 1





    +1. This answer is OK to me.

    – Polyfun
    Aug 28 '12 at 16:30
















1














In a larger program this would not be a problem as the DevideByZero exception would "bubble-up" and would hopefully be dealt with somewhere else. Because this is in the main method, the exception has no where to go. This causes the problem you see...



So the following would do as you expect



static void Main(string args)
{
try
{
CatchTest();
}
catch (Exception)
{

}
}

private static void CatchTest()
{
int i = 0;
try
{
int j = 1 / i; // Generate a divide by 0 exception.
}
finally
{
Console.Out.WriteLine("Finished");
Console.In.ReadLine();
}
}


I hope this helps.






share|improve this answer





















  • 2





    Downvoter care to comment?

    – MoonKnight
    Aug 28 '12 at 16:17






  • 1





    +1. This answer is OK to me.

    – Polyfun
    Aug 28 '12 at 16:30














1












1








1







In a larger program this would not be a problem as the DevideByZero exception would "bubble-up" and would hopefully be dealt with somewhere else. Because this is in the main method, the exception has no where to go. This causes the problem you see...



So the following would do as you expect



static void Main(string args)
{
try
{
CatchTest();
}
catch (Exception)
{

}
}

private static void CatchTest()
{
int i = 0;
try
{
int j = 1 / i; // Generate a divide by 0 exception.
}
finally
{
Console.Out.WriteLine("Finished");
Console.In.ReadLine();
}
}


I hope this helps.






share|improve this answer















In a larger program this would not be a problem as the DevideByZero exception would "bubble-up" and would hopefully be dealt with somewhere else. Because this is in the main method, the exception has no where to go. This causes the problem you see...



So the following would do as you expect



static void Main(string args)
{
try
{
CatchTest();
}
catch (Exception)
{

}
}

private static void CatchTest()
{
int i = 0;
try
{
int j = 1 / i; // Generate a divide by 0 exception.
}
finally
{
Console.Out.WriteLine("Finished");
Console.In.ReadLine();
}
}


I hope this helps.







share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 28 '12 at 16:31

























answered Aug 28 '12 at 16:07









MoonKnightMoonKnight

16.6k26112211




16.6k26112211








  • 2





    Downvoter care to comment?

    – MoonKnight
    Aug 28 '12 at 16:17






  • 1





    +1. This answer is OK to me.

    – Polyfun
    Aug 28 '12 at 16:30














  • 2





    Downvoter care to comment?

    – MoonKnight
    Aug 28 '12 at 16:17






  • 1





    +1. This answer is OK to me.

    – Polyfun
    Aug 28 '12 at 16:30








2




2





Downvoter care to comment?

– MoonKnight
Aug 28 '12 at 16:17





Downvoter care to comment?

– MoonKnight
Aug 28 '12 at 16:17




1




1





+1. This answer is OK to me.

– Polyfun
Aug 28 '12 at 16:30





+1. This answer is OK to me.

– Polyfun
Aug 28 '12 at 16:30











0














You still need a catch to capture the exception throwing:



int i = 0;
try
{
int j = 1 / i; // Generate a divide by 0 exception.
}
catch(Exception e)
{
Console.Out.WriteLine("Exception caught");
}
finally
{

Console.Out.WriteLine("Finished");
Console.In.ReadLine();
}


This exception is causing the application to crash, and the exception is unhandled therefore terminating the application.



http://msdn.microsoft.com/en-us/library/zwc8s4fz(v=vs.100).aspx




Usually, when an unhandled exception ends an application, whether or not the finally block is run is not important. However, if you have statements in a finally block that must be run even in that situation, one solution is to add a catch block to the try-finally statement.







share|improve this answer


























  • No because I'm trying to use try-finally: msdn.microsoft.com/en-us/library/zwc8s4fz%28v=vs.71%29.aspx

    – Fr33dan
    Aug 28 '12 at 16:01











  • Again, when there's an error you never get to the finally.

    – carny666
    Aug 28 '12 at 16:07











  • No. This is not right. The finally block will always get called as long as the exception has somewhere to go. In this case it doesn't...

    – MoonKnight
    Aug 28 '12 at 16:11











  • @carny666 it's happening because he's running it in a debugger, if he runs it in release mode the finally will be executed. See this question for more details: stackoverflow.com/questions/3421738/… application-while-using-f5

    – Kiril
    Aug 28 '12 at 16:11











  • @Lirik yup.. I get that now.

    – carny666
    Aug 28 '12 at 16:12
















0














You still need a catch to capture the exception throwing:



int i = 0;
try
{
int j = 1 / i; // Generate a divide by 0 exception.
}
catch(Exception e)
{
Console.Out.WriteLine("Exception caught");
}
finally
{

Console.Out.WriteLine("Finished");
Console.In.ReadLine();
}


This exception is causing the application to crash, and the exception is unhandled therefore terminating the application.



http://msdn.microsoft.com/en-us/library/zwc8s4fz(v=vs.100).aspx




Usually, when an unhandled exception ends an application, whether or not the finally block is run is not important. However, if you have statements in a finally block that must be run even in that situation, one solution is to add a catch block to the try-finally statement.







share|improve this answer


























  • No because I'm trying to use try-finally: msdn.microsoft.com/en-us/library/zwc8s4fz%28v=vs.71%29.aspx

    – Fr33dan
    Aug 28 '12 at 16:01











  • Again, when there's an error you never get to the finally.

    – carny666
    Aug 28 '12 at 16:07











  • No. This is not right. The finally block will always get called as long as the exception has somewhere to go. In this case it doesn't...

    – MoonKnight
    Aug 28 '12 at 16:11











  • @carny666 it's happening because he's running it in a debugger, if he runs it in release mode the finally will be executed. See this question for more details: stackoverflow.com/questions/3421738/… application-while-using-f5

    – Kiril
    Aug 28 '12 at 16:11











  • @Lirik yup.. I get that now.

    – carny666
    Aug 28 '12 at 16:12














0












0








0







You still need a catch to capture the exception throwing:



int i = 0;
try
{
int j = 1 / i; // Generate a divide by 0 exception.
}
catch(Exception e)
{
Console.Out.WriteLine("Exception caught");
}
finally
{

Console.Out.WriteLine("Finished");
Console.In.ReadLine();
}


This exception is causing the application to crash, and the exception is unhandled therefore terminating the application.



http://msdn.microsoft.com/en-us/library/zwc8s4fz(v=vs.100).aspx




Usually, when an unhandled exception ends an application, whether or not the finally block is run is not important. However, if you have statements in a finally block that must be run even in that situation, one solution is to add a catch block to the try-finally statement.







share|improve this answer















You still need a catch to capture the exception throwing:



int i = 0;
try
{
int j = 1 / i; // Generate a divide by 0 exception.
}
catch(Exception e)
{
Console.Out.WriteLine("Exception caught");
}
finally
{

Console.Out.WriteLine("Finished");
Console.In.ReadLine();
}


This exception is causing the application to crash, and the exception is unhandled therefore terminating the application.



http://msdn.microsoft.com/en-us/library/zwc8s4fz(v=vs.100).aspx




Usually, when an unhandled exception ends an application, whether or not the finally block is run is not important. However, if you have statements in a finally block that must be run even in that situation, one solution is to add a catch block to the try-finally statement.








share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 28 '12 at 16:08

























answered Aug 28 '12 at 16:01









TomTom

1,9471514




1,9471514













  • No because I'm trying to use try-finally: msdn.microsoft.com/en-us/library/zwc8s4fz%28v=vs.71%29.aspx

    – Fr33dan
    Aug 28 '12 at 16:01











  • Again, when there's an error you never get to the finally.

    – carny666
    Aug 28 '12 at 16:07











  • No. This is not right. The finally block will always get called as long as the exception has somewhere to go. In this case it doesn't...

    – MoonKnight
    Aug 28 '12 at 16:11











  • @carny666 it's happening because he's running it in a debugger, if he runs it in release mode the finally will be executed. See this question for more details: stackoverflow.com/questions/3421738/… application-while-using-f5

    – Kiril
    Aug 28 '12 at 16:11











  • @Lirik yup.. I get that now.

    – carny666
    Aug 28 '12 at 16:12



















  • No because I'm trying to use try-finally: msdn.microsoft.com/en-us/library/zwc8s4fz%28v=vs.71%29.aspx

    – Fr33dan
    Aug 28 '12 at 16:01











  • Again, when there's an error you never get to the finally.

    – carny666
    Aug 28 '12 at 16:07











  • No. This is not right. The finally block will always get called as long as the exception has somewhere to go. In this case it doesn't...

    – MoonKnight
    Aug 28 '12 at 16:11











  • @carny666 it's happening because he's running it in a debugger, if he runs it in release mode the finally will be executed. See this question for more details: stackoverflow.com/questions/3421738/… application-while-using-f5

    – Kiril
    Aug 28 '12 at 16:11











  • @Lirik yup.. I get that now.

    – carny666
    Aug 28 '12 at 16:12

















No because I'm trying to use try-finally: msdn.microsoft.com/en-us/library/zwc8s4fz%28v=vs.71%29.aspx

– Fr33dan
Aug 28 '12 at 16:01





No because I'm trying to use try-finally: msdn.microsoft.com/en-us/library/zwc8s4fz%28v=vs.71%29.aspx

– Fr33dan
Aug 28 '12 at 16:01













Again, when there's an error you never get to the finally.

– carny666
Aug 28 '12 at 16:07





Again, when there's an error you never get to the finally.

– carny666
Aug 28 '12 at 16:07













No. This is not right. The finally block will always get called as long as the exception has somewhere to go. In this case it doesn't...

– MoonKnight
Aug 28 '12 at 16:11





No. This is not right. The finally block will always get called as long as the exception has somewhere to go. In this case it doesn't...

– MoonKnight
Aug 28 '12 at 16:11













@carny666 it's happening because he's running it in a debugger, if he runs it in release mode the finally will be executed. See this question for more details: stackoverflow.com/questions/3421738/… application-while-using-f5

– Kiril
Aug 28 '12 at 16:11





@carny666 it's happening because he's running it in a debugger, if he runs it in release mode the finally will be executed. See this question for more details: stackoverflow.com/questions/3421738/… application-while-using-f5

– Kiril
Aug 28 '12 at 16:11













@Lirik yup.. I get that now.

– carny666
Aug 28 '12 at 16:12





@Lirik yup.. I get that now.

– carny666
Aug 28 '12 at 16:12











0














You need to detach the debugger (e.g. run your application in Release mode), see the following (related) question for more details: finally doesn't seem to execute in C# console application while using F5






share|improve this answer


























  • I actually mention this issue and reference that question in my question. I've tried running it outside the debugger.

    – Fr33dan
    Aug 28 '12 at 16:25
















0














You need to detach the debugger (e.g. run your application in Release mode), see the following (related) question for more details: finally doesn't seem to execute in C# console application while using F5






share|improve this answer


























  • I actually mention this issue and reference that question in my question. I've tried running it outside the debugger.

    – Fr33dan
    Aug 28 '12 at 16:25














0












0








0







You need to detach the debugger (e.g. run your application in Release mode), see the following (related) question for more details: finally doesn't seem to execute in C# console application while using F5






share|improve this answer















You need to detach the debugger (e.g. run your application in Release mode), see the following (related) question for more details: finally doesn't seem to execute in C# console application while using F5







share|improve this answer














share|improve this answer



share|improve this answer








edited May 23 '17 at 11:55









Community

11




11










answered Aug 28 '12 at 16:10









KirilKiril

28.8k23144208




28.8k23144208













  • I actually mention this issue and reference that question in my question. I've tried running it outside the debugger.

    – Fr33dan
    Aug 28 '12 at 16:25



















  • I actually mention this issue and reference that question in my question. I've tried running it outside the debugger.

    – Fr33dan
    Aug 28 '12 at 16:25

















I actually mention this issue and reference that question in my question. I've tried running it outside the debugger.

– Fr33dan
Aug 28 '12 at 16:25





I actually mention this issue and reference that question in my question. I've tried running it outside the debugger.

– Fr33dan
Aug 28 '12 at 16:25


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12163149%2ffinally-not-called-after-try%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas