Solved

C#: How to ignore an exception and continue processing

Posted on 2008-10-14
9
8,685 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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:Tony McCreath
Tony McCreath 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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