0

i am trying to handle exception and save that exception in database

Function1()  
{  
    try  
    {  
        for(int i=0;i<dt.rows.count;i++)  
        {  
            Function2();  
        }  
    }  
    catch(exception ex)  
    {  
        saveInDB(ex.message.tostring(),id);  
    }  

}  

Function2()  
{  
    try  
    {  
        function3()  
    }  
    catch(exception ex)  
    {  
        throw ex;  
    }  
}  

Function3()  
{  
    try  
    {  
        function4()  
    }  
    catch(exception ex)  
    {  
        throw ex;  
    }  
}  

Function4()  
{  
    try  
    {  
        code;  
    }  
    catch(exception ex)  
    {  
        throw ex;  
    }  
}  

suppose i got a exception in method4 then it will throw it to function3->Function2->function1 and then function1 will write exception in database.

but after writing exception in DB i want to continue for loop.

so how should i do?

1
  • Why do you have a function that only starts an other function? Commented Feb 19, 2013 at 9:47

4 Answers 4

1

but after writing exception in DB i want to continue for loop

That is only possible by putting an (extra) try/catch inside the for loop. You should do so only if the next round of the loop is independent and you are certain there is no harm done to your system.

Ask yourself: After an unknown error, do I still dare to write business-data to the database?

Notice that your code is in violation of some best-practices:

  • throw ex; resets the stack-trace. Replace it with throw;
  • when the catch blocks in function2 - function4 don't do anything with the exceptions, remove the try/catch altogether.
Sign up to request clarification or add additional context in comments.

Comments

1

You can put your try-catch into the for loop's body:

Function1()  
{  
    for(int i=0;i<dt.rows.count;i++)  
    {  
        try  
        {  
            Function2();  
        }  
        catch(Exception ex)  
        {  
            saveInDB(ex.message.tostring(),id);  
        }  
    }
}  

Keep in mind, however, that IO such as saving to DB may be pretty unreliable (and slow). This in turn might lead to further exception being thrown in your catch, which will tear down your loop.

Therefore it might be better to store all thrown exception in a data structure and dump them to the DB at once. This way the loop runs for each and every row.

Function1()  
{
    var errors = new LinkedList<Exception>();
    for(int i=0;i<dt.rows.count;i++)  
    {  
        try  
        {  
            Function2();  
        }  
        catch(Exception ex)  
        {  
            errors.AddLast(ex);
        }  
    }
    if(errors.Count > 0)
    {        
        // now you got all exception in errors and can dump
        // them in one block
    }
}

2 Comments

@TruptiGhorpade Nice to read; could you accept an answer or post the solution to your problem?
I'm not sure I'd I would do the second part and add them to a list. But it would be best to just catch it if you can handle it. Also for logging I'd recommend this oss project that I work on: github.com/exceptionless/Exceptionless It's tested and free.
0

As long as any try-catch block throws the exception, the program cannot continue. If you want your program to be continue after exception handling, my suggestion would be not to use:

throw ex;

Comments

0

If you need to continue looping after an exception within the loop, you need to add a try/catch block within the loop as below;

try{

    for(int i=0; i<10; i++){
       try{
         //do your work here
       }
       catch (Exception e){
         //write to db here and then it will continue in the for loop
       }
    }

    //rest of the code
}
catch (Exception ex){
  //write to db
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.