Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have read various article throwing and handling an exception is usually an expensive operation. i have reviewed code of many developer in my organisation and found that most of developer user this type code is given below

C#
private void RunnSqlLoader()
    {
        string sp1 = _strPath + _strFileName + ".bat";

        try
        {

            ProcessStartInfo startInfo = new ProcessStartInfo(@sp1)
            {
                WorkingDirectory = _strPath,
                CreateNoWindow = false,
                WindowStyle = ProcessWindowStyle.Hidden,
                UseShellExecute = false
            };
            Process batchExecute = new Process { StartInfo = startInfo };
            batchExecute.Start();
            batchExecute.WaitForExit();
        }
        catch (Exception ex)
        {
            throw ex;
        }


    }


so Is this code use better exception handling ?
or my point of view its not useful here. but i am not sure about?


please anybody explain me
Posted
Updated 8-Oct-15 0:18am
v2
Comments
Philippe Mori 8-Oct-15 12:43pm    
If you want to better understand proper exception handling, then this code is not a good starting point as it is pointless to catch an exception and fthrow it back without doing anything else...

That code is exception handling, but pretty pointless handling. If all you are doing is re-throwing the exception then don't bother with the try\catch block. If you are going to rethrow then use just "throw" and not "throw ex" and you lose some exception context with you throw a new exception via "throw ex".
 
Share this answer
 
It looks the original developer wanted to handle the exceptions but postponed the task (hence the re-throw). In any case the resulting code is just ugly.
 
Share this answer
 
This is just the basic syntax to handle the Exception and there are another ways to handle the exception but this is mostly used..

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
// Your code which is to be execute
}
catch (Exception ex)
{
// Log the exception

}
finally
{
// for dispose the Process/ Task

}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900