[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Nested try/catches

Posted on 2006-06-08
3
Medium Priority
?
299 Views
Last Modified: 2010-04-16
I have a line of code that I need to put in a try/catch because there are instances where I get a security error. I need to do it in a way that the variable can be used later without getting an error that it isn't defined. Here is the the line:

string[] newFiles = toPublishDirectoryMonitor.NewFiles;

The other code I have is below. My statement above needs to be before the foreach statement. I can I add this without an error?

foreach (string newFile in newFiles)
            {
                PublishTaskDataSet dsTask;
                try
                {
                    dsTask = new PublishTaskDataSet();
                    dsTask.ReadXml(newFile);
                    string jobName;
                    try
                    {
                        jobName = dsTask.PublishTask[0].Name;
                    }
                    catch
                    {
                        jobName = "???";
                    }
                    DataSet dsJobInfo = JobCreator.CreateJob(jobName, Assembly.GetAssembly(typeof(PublisherJobPlugin)).Location, 1, currentUser);
                    JobInfo newJob = new JobInfo();
                    newJob.Filename = newFile;
                    newJob.Id = publishJobServer.AddJob(dsJobInfo, dsTask);
                    jobsList.Add(newJob);
                }
                catch (Exception ex)
                {
                    File.Move(newFile, newFile + ".Error");
                    StreamWriter swError = File.AppendText(newFile + ".Error");
                    swError.WriteLine(" ");
                    swError.WriteLine("Error reading publish task file: " + ex.Message);
                    swError.Close();
                    toPublishDirectoryMonitor.ResetFile(newFile);
                }
            }
0
Comment
Question by:lmred
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
WinterMuteUK earned 200 total points
ID: 16862711
Well,
the easiest way is to do it the same way you did the 'jobName' variable:

  string[] newFiles = null;

  try{ newFiles = toPublishDirectoryMonitor.NewFiles; }
  catch(SecurityException) {}

  if(newFiles == null) {/**/}
  else
    /*Your foreach loop.*/

Unless I've misunderstood what you want?

Wint.
0
 
LVL 2

Author Comment

by:lmred
ID: 16862770
Thanks so much. I was making it harder than it was!

Lmred
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 16862783
No worries - Glad to have helped.

Wint
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
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

825 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