Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

system.diagnostics.process.start error handling

Status
Not open for further replies.

tjbradford

Technical User
Dec 14, 2007
229
0
0
GB
System.Diagnostics.Process.Start(@"c:\windows\system32\calc.exe");

my code is running the above command calc.exe for example,
the problem is if the .exe isn't found it hangs the app, what i would like todo is just resume without it hanging if it encounters a problem, how can i do this with the above command.

I'm very new to c# so might not understand unless its written for someone simple :eek:)

thanks
 
try
{
// carry out your code here.
}
catch(Exception ex)
{
MessageBox.Show(Ex.Message);
}

thing this sorted it

good practice ?
 
it works and you will need some form of error handling. I would add code to ensure the program does exist before running. I would also display the complete exception detail in the message box, as this will inform you of what the exception is and where it occurred.
Code:
try
{
   var program = @"c:\windows\system32\calc.exe";
   if(!File.Exists(program))
   {
      MessageBox.Show(program + " does not exist.");
      return;
   }
   Process.Start(program);
}
catch(Exception exception)
{
   MessageBox.Show(exception.ToString());
}
usually you do not want to report the actual exception to the end user. instead you would log the exception (using log4net or another logging framework) and guide the user through a friendlier "a error occurred" user experience.

that could be a simple as a message stating "an error

occurred and was logged.". To a full blown workflow that guides the user to correct why the error occurred. how complex the system is drives the best approach for handling exceptions.

there are also guidelines on for handling exceptions. these guidelines are language agnostic. so they apply to all OOP languages. if you're interested a google "exception handling practices."

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top