PC Review


Reply
Thread Tools Rate Thread

Catch process exception

 
 
Eran.Yasso@gmail.com
Guest
Posts: n/a
 
      26th Feb 2007
Hi,

My app starts process. Some times this process exits because of
exception. Can my app know if the process exited due to exception or
gracefully?

In both ways, the exit code of this process is zero.

I tried using the following, but it goes to catch.

ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();
try
{
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
TestProcess.StartInfo = myProcessStartInfo;
TestProcess = Process.Start( MyProcess , " -file "+
cfrmTRSobj.GetTRSFullFileName() );
SetProcID( TestProcess );
TestProcess.WaitForExit();
StreamReader myStreamReader = TestProcess.StandardError;
MessageBox.Show(myStreamReader.ReadToEnd());
}
catch( Exception e )
{
bRetval = frmMain.RETVALSTATUS.FAIL;
TestStatus = frmMain.DGETestStatusenums.STATUSFAILED;
}
when the app reached to "StreamReader myStreamReader =
TestProcess.StandardError;" it jump to catch.

Is there any other way or am I doing any thing wrong?

 
Reply With Quote
 
 
 
 
Eran.Yasso@gmail.com
Guest
Posts: n/a
 
      26th Feb 2007
On Feb 26, 4:42 pm, Eran.Ya...@gmail.com wrote:
> Hi,
>
> My app starts process. Some times this process exits because of
> exception. Can my app know if the process exited due to exception or
> gracefully?
>
> In both ways, the exit code of this process is zero.
>
> I tried using the following, but it goes to catch.
>
> ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();
> try
> {
> myProcessStartInfo.UseShellExecute = false;
> myProcessStartInfo.RedirectStandardError = true;
> TestProcess.StartInfo = myProcessStartInfo;
> TestProcess = Process.Start( MyProcess , " -file "+
> cfrmTRSobj.GetTRSFullFileName() );
> SetProcID( TestProcess );
> TestProcess.WaitForExit();
> StreamReader myStreamReader = TestProcess.StandardError;
> MessageBox.Show(myStreamReader.ReadToEnd());}
>
> catch( Exception e )
> {
> bRetval = frmMain.RETVALSTATUS.FAIL;
> TestStatus = frmMain.DGETestStatusenums.STATUSFAILED;}
>
> when the app reached to "StreamReader myStreamReader =
> TestProcess.StandardError;" it jump to catch.
>
> Is there any other way or am I doing any thing wrong?


Ooops, forgot. Thanks for the help.

 
Reply With Quote
 
sloan
Guest
Posts: n/a
 
      26th Feb 2007

You need to stop mixing "API , was it Zero or something else" mentality with
Exception Handling.


Below is a stab at a rewrite/refactor.

Handle the exception ( a specific exception like IOException and not
"Exception" on the outside calling code.




private string RunTheProcess(SomeObject cfrmTRSobj)
{


StreamReader myStreamReader = null;
ProcessStartInfo myProcessStartInfo = null;
try
{


myProcessStartInfo = new ProcessStartInfo();

myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
TestProcess.StartInfo = myProcessStartInfo;
TestProcess = Process.Start( MyProcess , " -file "+
cfrmTRSobj.GetTRSFullFileName() );
SetProcID( TestProcess );
TestProcess.WaitForExit();
StreamReader myStreamReader = TestProcess.StandardError; //HUH???
return myStreamReader.ReadToEnd();

}

// purposely, there is no catch statement here


finally
{
//clean up here
if(null!=myStreamReader)
{
myStreamReader.Close();
}
}


}

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Feb 26, 4:42 pm, Eran.Ya...@gmail.com wrote:
> > Hi,
> >
> > My app starts process. Some times this process exits because of
> > exception. Can my app know if the process exited due to exception or
> > gracefully?
> >
> > In both ways, the exit code of this process is zero.
> >
> > I tried using the following, but it goes to catch.
> >
> > ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();
> > try
> > {
> > myProcessStartInfo.UseShellExecute = false;
> > myProcessStartInfo.RedirectStandardError = true;
> > TestProcess.StartInfo = myProcessStartInfo;
> > TestProcess = Process.Start( MyProcess , " -file "+
> > cfrmTRSobj.GetTRSFullFileName() );
> > SetProcID( TestProcess );
> > TestProcess.WaitForExit();
> > StreamReader myStreamReader = TestProcess.StandardError;
> > MessageBox.Show(myStreamReader.ReadToEnd());}
> >
> > catch( Exception e )
> > {
> > bRetval = frmMain.RETVALSTATUS.FAIL;
> > TestStatus = frmMain.DGETestStatusenums.STATUSFAILED;}
> >
> > when the app reached to "StreamReader myStreamReader =
> > TestProcess.StandardError;" it jump to catch.
> >
> > Is there any other way or am I doing any thing wrong?

>
> Ooops, forgot. Thanks for the help.
>



 
Reply With Quote
 
Eran.Yasso@gmail.com
Guest
Posts: n/a
 
      26th Feb 2007
On Feb 26, 5:31 pm, "sloan" <s...@ipass.net> wrote:
> You need to stop mixing "API , was it Zero or something else" mentality with
> Exception Handling.
>
> Below is a stab at a rewrite/refactor.
>
> Handle the exception ( a specific exception like IOException and not
> "Exception" on the outside calling code.
>
> private string RunTheProcess(SomeObject cfrmTRSobj)
> {
>
> StreamReader myStreamReader = null;
> ProcessStartInfo myProcessStartInfo = null;
> try
> {
>
> myProcessStartInfo = new ProcessStartInfo();
>
> myProcessStartInfo.UseShellExecute = false;
> myProcessStartInfo.RedirectStandardError = true;
> TestProcess.StartInfo = myProcessStartInfo;
> TestProcess = Process.Start( MyProcess , " -file "+
> cfrmTRSobj.GetTRSFullFileName() );
> SetProcID( TestProcess );
> TestProcess.WaitForExit();
> StreamReader myStreamReader = TestProcess.StandardError; //HUH???
> return myStreamReader.ReadToEnd();
>
> }
>
> // purposely, there is no catch statement here
>
> finally
> {
> //clean up here
> if(null!=myStreamReader)
> {
> myStreamReader.Close();
>
> }
> }
> }
> <Eran.Ya...@gmail.com> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
> > On Feb 26, 4:42 pm, Eran.Ya...@gmail.com wrote:
> > > Hi,

>
> > > My app starts process. Some times this process exits because of
> > > exception. Can my app know if the process exited due to exception or
> > > gracefully?

>
> > > In both ways, the exit code of this process is zero.

>
> > > I tried using the following, but it goes to catch.

>
> > > ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();
> > > try
> > > {
> > > myProcessStartInfo.UseShellExecute = false;
> > > myProcessStartInfo.RedirectStandardError = true;
> > > TestProcess.StartInfo = myProcessStartInfo;
> > > TestProcess = Process.Start( MyProcess , " -file "+
> > > cfrmTRSobj.GetTRSFullFileName() );
> > > SetProcID( TestProcess );
> > > TestProcess.WaitForExit();
> > > StreamReader myStreamReader = TestProcess.StandardError;
> > > MessageBox.Show(myStreamReader.ReadToEnd());}

>
> > > catch( Exception e )
> > > {
> > > bRetval = frmMain.RETVALSTATUS.FAIL;
> > > TestStatus = frmMain.DGETestStatusenums.STATUSFAILED;}

>
> > > when the app reached to "StreamReader myStreamReader =
> > > TestProcess.StandardError;" it jump to catch.

>
> > > Is there any other way or am I doing any thing wrong?

>
> > Ooops, forgot. Thanks for the help. - Hide quoted text -

>
> - Show quoted text -


Hi, sloan.

I took the sample from MSDN.


Process myProcess = new Process();

//I choose not to send parameters to the constructor
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("net ","use
"+ args[0]);

myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();

StreamReader myStreamReader = myProcess.StandardError; //<------HUH???
// Read the standard error of net.exe and write it on to console.
Console.WriteLine( myStreamReader.ReadLine());
myProcess.Close();


 
Reply With Quote
 
Ben Voigt
Guest
Posts: n/a
 
      26th Feb 2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> My app starts process. Some times this process exits because of
> exception. Can my app know if the process exited due to exception or
> gracefully?
>
> In both ways, the exit code of this process is zero.
>
> I tried using the following, but it goes to catch.
>
> ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();
> try
> {
> myProcessStartInfo.UseShellExecute = false;
> myProcessStartInfo.RedirectStandardError = true;
> TestProcess.StartInfo = myProcessStartInfo;
> TestProcess = Process.Start( MyProcess , " -file "+
> cfrmTRSobj.GetTRSFullFileName() );
> SetProcID( TestProcess );
> TestProcess.WaitForExit();
> StreamReader myStreamReader = TestProcess.StandardError;
> MessageBox.Show(myStreamReader.ReadToEnd());
> }
> catch( Exception e )
> {
> bRetval = frmMain.RETVALSTATUS.FAIL;
> TestStatus = frmMain.DGETestStatusenums.STATUSFAILED;
> }
> when the app reached to "StreamReader myStreamReader =
> TestProcess.StandardError;" it jump to catch.
>
> Is there any other way or am I doing any thing wrong?
>


From the documentation:

p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected error stream.
// p.WaitForExit();
// Read the error stream first and then wait.
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
The code example avoids a deadlock condition by calling
p.StandardError.ReadToEnd before p.WaitForExit. A deadlock condition can
result if the parent process calls p.WaitForExit before
p.StandardError.ReadToEnd



 
Reply With Quote
 
Eran.Yasso@gmail.com
Guest
Posts: n/a
 
      26th Feb 2007
On Feb 26, 7:12 pm, "Ben Voigt" <r...@nospam.nospam> wrote:
> <Eran.Ya...@gmail.com> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
>
>
> > Hi,

>
> > My app starts process. Some times this process exits because of
> > exception. Can my app know if the process exited due to exception or
> > gracefully?

>
> > In both ways, the exit code of this process is zero.

>
> > I tried using the following, but it goes to catch.

>
> > ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();
> > try
> > {
> > myProcessStartInfo.UseShellExecute = false;
> > myProcessStartInfo.RedirectStandardError = true;
> > TestProcess.StartInfo = myProcessStartInfo;
> > TestProcess = Process.Start( MyProcess , " -file "+
> > cfrmTRSobj.GetTRSFullFileName() );
> > SetProcID( TestProcess );
> > TestProcess.WaitForExit();
> > StreamReader myStreamReader = TestProcess.StandardError;
> > MessageBox.Show(myStreamReader.ReadToEnd());
> > }
> > catch( Exception e )
> > {
> > bRetval = frmMain.RETVALSTATUS.FAIL;
> > TestStatus = frmMain.DGETestStatusenums.STATUSFAILED;
> > }
> > when the app reached to "StreamReader myStreamReader =
> > TestProcess.StandardError;" it jump to catch.

>
> > Is there any other way or am I doing any thing wrong?

>
> From the documentation:
>
> p.Start();
> // Do not wait for the child process to exit before
> // reading to the end of its redirected error stream.
> // p.WaitForExit();
> // Read the error stream first and then wait.
> string error = p.StandardError.ReadToEnd();
> p.WaitForExit();
> The code example avoids a deadlock condition by calling
> p.StandardError.ReadToEnd before p.WaitForExit. A deadlock condition can
> result if the parent process calls p.WaitForExit before
> p.StandardError.ReadToEnd- Hide quoted text -
>
> - Show quoted text -


Can you send me a link to that please?

 
Reply With Quote
 
Eran.Yasso@gmail.com
Guest
Posts: n/a
 
      26th Feb 2007
On Feb 26, 7:12 pm, "Ben Voigt" <r...@nospam.nospam> wrote:
> <Eran.Ya...@gmail.com> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
>
>
> > Hi,

>
> > My app starts process. Some times this process exits because of
> > exception. Can my app know if the process exited due to exception or
> > gracefully?

>
> > In both ways, the exit code of this process is zero.

>
> > I tried using the following, but it goes to catch.

>
> > ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();
> > try
> > {
> > myProcessStartInfo.UseShellExecute = false;
> > myProcessStartInfo.RedirectStandardError = true;
> > TestProcess.StartInfo = myProcessStartInfo;
> > TestProcess = Process.Start( MyProcess , " -file "+
> > cfrmTRSobj.GetTRSFullFileName() );
> > SetProcID( TestProcess );
> > TestProcess.WaitForExit();
> > StreamReader myStreamReader = TestProcess.StandardError;
> > MessageBox.Show(myStreamReader.ReadToEnd());
> > }
> > catch( Exception e )
> > {
> > bRetval = frmMain.RETVALSTATUS.FAIL;
> > TestStatus = frmMain.DGETestStatusenums.STATUSFAILED;
> > }
> > when the app reached to "StreamReader myStreamReader =
> > TestProcess.StandardError;" it jump to catch.

>
> > Is there any other way or am I doing any thing wrong?

>
> From the documentation:
>
> p.Start();
> // Do not wait for the child process to exit before
> // reading to the end of its redirected error stream.
> // p.WaitForExit();
> // Read the error stream first and then wait.
> string error = p.StandardError.ReadToEnd();
> p.WaitForExit();
> The code example avoids a deadlock condition by calling
> p.StandardError.ReadToEnd before p.WaitForExit. A deadlock condition can
> result if the parent process calls p.WaitForExit before
> p.StandardError.ReadToEnd- Hide quoted text -
>
> - Show quoted text -


Hi,

Can you send me link please?

thanks.

 
Reply With Quote
 
Ben Voigt
Guest
Posts: n/a
 
      26th Feb 2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Feb 26, 7:12 pm, "Ben Voigt" <r...@nospam.nospam> wrote:
>> <Eran.Ya...@gmail.com> wrote in message
>>
>> news:(E-Mail Removed)...
>>
>>
>>
>>
>>
>> > Hi,

>>
>> > My app starts process. Some times this process exits because of
>> > exception. Can my app know if the process exited due to exception or
>> > gracefully?

>>
>> > In both ways, the exit code of this process is zero.

>>
>> > I tried using the following, but it goes to catch.

>>
>> > ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();
>> > try
>> > {
>> > myProcessStartInfo.UseShellExecute = false;
>> > myProcessStartInfo.RedirectStandardError = true;
>> > TestProcess.StartInfo = myProcessStartInfo;
>> > TestProcess = Process.Start( MyProcess , " -file "+
>> > cfrmTRSobj.GetTRSFullFileName() );
>> > SetProcID( TestProcess );
>> > TestProcess.WaitForExit();
>> > StreamReader myStreamReader = TestProcess.StandardError;
>> > MessageBox.Show(myStreamReader.ReadToEnd());
>> > }
>> > catch( Exception e )
>> > {
>> > bRetval = frmMain.RETVALSTATUS.FAIL;
>> > TestStatus = frmMain.DGETestStatusenums.STATUSFAILED;
>> > }
>> > when the app reached to "StreamReader myStreamReader =
>> > TestProcess.StandardError;" it jump to catch.

>>
>> > Is there any other way or am I doing any thing wrong?

>>
>> From the documentation:
>>
>> p.Start();
>> // Do not wait for the child process to exit before
>> // reading to the end of its redirected error stream.
>> // p.WaitForExit();
>> // Read the error stream first and then wait.
>> string error = p.StandardError.ReadToEnd();
>> p.WaitForExit();
>> The code example avoids a deadlock condition by calling
>> p.StandardError.ReadToEnd before p.WaitForExit. A deadlock condition can
>> result if the parent process calls p.WaitForExit before
>> p.StandardError.ReadToEnd- Hide quoted text -
>>
>> - Show quoted text -

>
> Can you send me a link to that please?
>


http://msdn2.microsoft.com/en-us/lib...darderror.aspx


 
Reply With Quote
 
Eran.Yasso@gmail.com
Guest
Posts: n/a
 
      27th Feb 2007
On Feb 26, 11:15 pm, "Ben Voigt" <r...@nospam.nospam> wrote:
> <Eran.Ya...@gmail.com> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
>
>
> > On Feb 26, 7:12 pm, "Ben Voigt" <r...@nospam.nospam> wrote:
> >> <Eran.Ya...@gmail.com> wrote in message

>
> >>news:(E-Mail Removed)...

>
> >> > Hi,

>
> >> > My app starts process. Some times this process exits because of
> >> > exception. Can my app know if the process exited due to exception or
> >> > gracefully?

>
> >> > In both ways, the exit code of this process is zero.

>
> >> > I tried using the following, but it goes to catch.

>
> >> > ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();
> >> > try
> >> > {
> >> > myProcessStartInfo.UseShellExecute = false;
> >> > myProcessStartInfo.RedirectStandardError = true;
> >> > TestProcess.StartInfo = myProcessStartInfo;
> >> > TestProcess = Process.Start( MyProcess , " -file "+
> >> > cfrmTRSobj.GetTRSFullFileName() );
> >> > SetProcID( TestProcess );
> >> > TestProcess.WaitForExit();
> >> > StreamReader myStreamReader = TestProcess.StandardError;
> >> > MessageBox.Show(myStreamReader.ReadToEnd());
> >> > }
> >> > catch( Exception e )
> >> > {
> >> > bRetval = frmMain.RETVALSTATUS.FAIL;
> >> > TestStatus = frmMain.DGETestStatusenums.STATUSFAILED;
> >> > }
> >> > when the app reached to "StreamReader myStreamReader =
> >> > TestProcess.StandardError;" it jump to catch.

>
> >> > Is there any other way or am I doing any thing wrong?

>
> >> From the documentation:

>
> >> p.Start();
> >> // Do not wait for the child process to exit before
> >> // reading to the end of its redirected error stream.
> >> // p.WaitForExit();
> >> // Read the error stream first and then wait.
> >> string error = p.StandardError.ReadToEnd();
> >> p.WaitForExit();
> >> The code example avoids a deadlock condition by calling
> >> p.StandardError.ReadToEnd before p.WaitForExit. A deadlock condition can
> >> result if the parent process calls p.WaitForExit before
> >> p.StandardError.ReadToEnd- Hide quoted text -

>
> >> - Show quoted text -

>
> > Can you send me a link to that please?

>
> http://msdn2.microsoft.com/en-us/lib....process.s...- Hide quoted text -
>
> - Show quoted text -


Thanks, but can I use this method to get ending reason of
app(exception,user ending, etc...).

 
Reply With Quote
 
Ben Voigt
Guest
Posts: n/a
 
      27th Feb 2007
>
> Thanks, but can I use this method to get ending reason of
> app(exception,user ending, etc...).
>


From the code you provided, I was under the impression you were trying to
capture an error message the app writes to stderr. Is that accurate?

According to the documentation for GetExitCodeProcess, an application that
ends with an unhandled exception should terminate, and the exit code should
be the exception. But maybe the exception isn't unhandled, and the
application catches it and exits normally... in that case your only option
is to check for any error event the application might leave (could be a log
file, Windows Event Log, message on stdout or stderr, etc.)


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between try{}catch{} and try{}catch(Exception e){} ? Mr Flibble Microsoft C# .NET 7 22nd Jun 2006 04:25 PM
Catch the state of a process and if that process is receiving any type of input Filipe Marcelino Microsoft VB .NET 0 21st Jul 2005 12:13 PM
Process.GetProcesses() throws exception: process performance counter is disabled Raj Microsoft Dot NET 0 7th Oct 2003 09:50 PM
Process.GetProcesses() throws exception: process performance counter is disabled Raj Microsoft Dot NET Framework 0 7th Oct 2003 09:50 PM
How to catch COM exception in .Net Franck Microsoft ASP .NET 1 7th Aug 2003 07:43 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:01 PM.