locked
Try Catch Question RRS feed

  • Question

  • User-640323567 posted

    Hi,

    I am learning C#. I have a question regarding try/catch blocks.

    How do I exit out of catch block completely? I do not want any code to execute after catch block. 

    Let's say, I have something like this:

    foreach(DataRow rowItem in Rows){
    //some code here
    try
    {
    //some code here
    }
    catch1()
    {
    //some code here
    }
    catch2()
    {
    //some code here
    }
    catch3()
    {
    //some code here

    console.ReadLine(); } }

    If I am in catch3, how can I exit out of the foreach loop? I tried debugging the code, when it reaches 'console.ReadLine();' statement in catch3 block, it displays the error in command prompt window and when I close cmd window, it stops executing the code. 

    In reality, I want to see once it is done in catch3 block, will it go to foreach statement or not, but debugging stops execution once I close cmd window. I couldn't figure out, can somebody please guide me? How do I exit code completely or to foreach loop from catch block.

    Thanks for any help.

    Thursday, August 2, 2018 1:24 PM

All replies

  • User-82109136 posted

    There are three keyword that are related to this:

    return: will exit the method

    continue: will stop processing of the current iteration of the for and continue with the next iteration. 

    break: will leave the for statement and resume processing at the first line following the for loop.

    Thursday, August 2, 2018 1:39 PM
  • User-369506445 posted

    hi

    you can use <g class="gr_ gr_19 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="19" data-gr-id="19">return</g> keyword

    foreach (DataRow rowItem in Rows)
                {
    //some code here
                    try
                    {
                           return;
                    }
                    catch1()
                    {
                           return;
                    }
                    catch2()
                    {
                           return;
                    }
                    catch3()
                    {
     
                        return;
                    }
                }

    Thursday, August 2, 2018 1:41 PM
  • User-1924028965 posted

    Hi, 

    Being a learning developer, I can see you are swallowing any errors occurred in your code using try..catch block and do not want to handle the error in the catch block. 

    This practice may haunt you in the future when you become a solid developer.

    Therefore at this stage include the following message as bear minimum error handling, so if you need to debug it's easier to find the error in the debug listener window in the visual studio. 

    Debug.WriteLine("message to display on the event listener")

    https://msdn.microsoft.com/en-us/library/system.diagnostics.debug.writeline(v=vs.110).aspx

    Coming back to your original question since your code block is wrapped inside the for loop, you have 3 options to exit from the catch block. 

    continue: will exit the current iteration of the loop and moves to the next iteration. 

    break: break can be used if you need to exit from for each loop upon receiving the exception

    return: will exit from the method where this code lies.

    foreach(DataRow rowItem in Rows){
    //some code here
    try
    {
    //some code here
    }
    catch1(Exception ex)
    {
    Debug.WriteLine(ex.Message);
    // following statement skip the current iteration and continue with the next iteration;
    continue;
    //some code here
    
    }
    catch2(Exception ex)
    {
    Debug.WriteLine(ex.Message);
    // following statement exist from the method where this code snippet is exist
    return;
    //some code here
    }
    catch3(Exception ex)
    {
    Debug.WriteLine(ex.Message);
    // following statement will exist from the foreach loop. therefore subsequent iteration will not be executed.
    break;
    //some code here
    
    console.ReadLine();
    }
    }

    Wednesday, August 8, 2018 10:22 AM
  • User-1003897979 posted

    Do not use Try/Catch for control flow in your program.  Try/Catch is for handling 'Exceptions'.  An Exception is something unexpected that happens - a disk drive fills up, the network is down - that sort of thing.  Invalid user input, for example, is not exceptional - you should expect to get invalid input.  Data from a database that is not needed is not exceptional - filter it out.

    Try/Catch is slow.  Try/Catch if used for control flow is hard for a maintainer to follow.

    Make sure that you really, really, really need to have all those Catch blocks.

    Validation checking - good.  Exceptions - bad.

    So … why are you going to the Catch block?  What is triggering the Exception?

    Monday, September 10, 2018 12:40 PM