Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C#: How to ignore an exception and continue processing

Posted on 2008-10-14
9
Medium Priority
?
8,951 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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 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

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

715 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