Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C#: How to ignore an exception and continue processing

Posted on 2008-10-14
9
Medium Priority
?
9,030 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:Joel Coehoorn
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 1600 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:Tony McCreath
Tony McCreath earned 400 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

886 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