Solved

C#: How to ignore an exception and continue processing

Posted on 2008-10-14
9
8,647 Views
Last Modified: 2013-12-17
Hi,
I am building a web application in which I have a method that processes information that I get from a text file. If I encounter an error, the method stops. I would like to process the data in the file until the end, whatever error I encounter in the way. For that I would like a mechanism that allows me to put the error I encouter in a String variable and then read that string when I get done processing all the file. How can I do that.

0
Comment
Question by:karakav
9 Comments
 
LVL 16

Expert Comment

by:Gyanendra Singh
ID: 22710460
ues Try catch block

Try

{
// Your code
}

Catch()
{
}
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22710532
how are you reading your data
if its in a loop then
while (true)
{
   try
   {
       //processing code here
    }
    catch (Exception Ex)
    {
        //process the exception here
        continue;
     }
}
0
 
LVL 8

Expert Comment

by:tiagosalgado
ID: 22710819
In your process code, add a try...catch block and append all exceptions to a string.
StringBuilder errors = new StringBuilder();
// begin of your process
while(...)
{
    try
    {
        // your code here
    }
    catch(Exception ex)
    {
        errors.AppendFormat("{0};", ex.Message);
    }
}
 
// read all errors using errors.ToString();

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 18

Expert Comment

by:jcoehoorn
ID: 22711214
The trick with a try/catch block is that you jump all the way to the catch.  If you want to continue processing, a large try block around everything probably won't do what you want.  You need several try catch blocks, one wrapping each exception that could be thrown at a very fine level.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22711492
>>>If I encounter an error, the method stops. I would like to process the data in the file until the end, whatever error I encounter in the way.
That will be a big "NO NO". Whenever your application tells you there is an error, you better handle it first. The only exception is that the "error" is what you already expect and you are very sure that you can igore it without causing any problems.
0
 
LVL 4

Author Comment

by:karakav
ID: 22718688
Actually I have many errors already handled to that point that any other error should not be allowed to stop the execution. That's why I want to catch them apart and then examine them at a latter time. I found the solution of tiagoSalgado viable except that I don't see how I will recuperate the content of the StringBuilder since it is in a method wich returns other ting than String.
0
 
LVL 8

Accepted Solution

by:
tiagosalgado earned 400 total points
ID: 22719135
In that case you can use a out parameter in your function
 

public string YourFunction(string param1, out string error)
{
    try
    {
        ...
    }
    catch( ... )
    {
        error += "your error";
    }
}
 
And call your function like this
 
string myerror;
YourFunction("your_parameter", myerror);
Console.Write(error);

Open in new window

0
 
LVL 23

Assisted Solution

by:Tiggerito
Tiggerito earned 100 total points
ID: 22720742
It might be worth organising your errors into a collection, and even tag them with the line number the error happened on.

Maybe by creating an Error class (with Line and Message properties) and returning a List<Error> collection as a parameter.
0
 
LVL 4

Author Closing Comment

by:karakav
ID: 31505844
Thanks a lot.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question