Solved

C#: How to ignore an exception and continue processing

Posted on 2008-10-14
9
8,577 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
Comment Utility
ues Try catch block

Try

{
// Your code
}

Catch()
{
}
0
 
LVL 26

Expert Comment

by:Anurag Thakur
Comment Utility
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
Comment Utility
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
 
LVL 18

Expert Comment

by:jcoehoorn
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 22

Expert Comment

by:prairiedog
Comment Utility
>>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks a lot.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now