Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what is the difference between these 2!
try
{
dosomething();
}
catch
{
throw;
}



difference between above and below one.

try
{
dosomething();
}
catch(System.Exception ex)
{
throw ex;
}
Posted
Updated 15-Mar-11 9:22am
v3
Comments
Sergey Alexandrovich Kryukov 15-Mar-11 15:23pm    
I put a fix to make it compile. (Also, see my Answer.)
--SA

Look at this codeproject article:

Exception Handling Best Practices in .NET[^]
 
Share this answer
 
Comments
Espen Harlinn 16-Mar-11 17:11pm    
A 5 - That article just about sums it up ...
Venkatesh Mookkan 25-Mar-11 7:15am    
5!
In practice both cases do exactly the same. Also, they are completely redundant, only waste CPU time.

Please find more detailed explanation in my comment to the (incorrect) Answer by avigodse.
Now this answer is improved. Avigodse probably understands correctly how exception works, only it requires additional explanation. Declaration ex is the last code sample of the Question suggests some processing can be added before re-throwing exception. Until it is not done, both samples do the same and completely redundant. See my comment to the mentioned Answer.

It's very important not to block propagation of exceptions up to stack, so in many cases the exception should be re-thrown. If special processing is needed, the specialized type of exception should be used (of a type derived from System.Exception). This type is the best to report all exceptions and/or log them or created exception dump to report problems for CodeProject Questions :-).

See my additional instructions on using exception:

How do i make a loop that will stop when a scrollbar reaches the bottom[^]
When i run an application an exception is caught how to handle this?[^]

I tried to list best practices.

—SA
 
Share this answer
 
v3
Comments
avigodse 16-Mar-11 7:19am    
Answer given above by me, might be not so descriptive and well referenced, but is not incorrect. I have quoted a word "specified", and that means it can be any type of specified exception, and in this case its System.Exception.
Plz refer improved answer.
No offense but word incorrect should be respelled otherwise.
Sergey Alexandrovich Kryukov 16-Mar-11 12:30pm    
I commented what's still wrong with you answer.
--SA
Espen Harlinn 16-Mar-11 17:21pm    
5ed!
Sergey Alexandrovich Kryukov 16-Mar-11 18:08pm    
Thank you.
Somebody might want to revenge me for bad votes but decent people like you and me never revenge. :-)
--SA
Hope Link1[^] and Link2[^] might help you.
 
Share this answer
 
Comments
Raj.rcr 15-Mar-11 6:00am    
Very good Link.. My 5
[no name] 15-Mar-11 6:04am    
Thanks Raj.
Raj.rcr 15-Mar-11 6:04am    
Mr.Ramalinga, I have been searching for a solution to export the gridview data into excel in 2007 format. I tried in many different ways, but till now I didn't get the solution for it. Even in this website also.. I hope u can help me.. Thanks

---
Raj
[no name] 15-Mar-11 6:05am    
What code have you tried, What exception or error it throws.
Raj.rcr 15-Mar-11 6:23am    
string fileName = "attachment;filename=OverallReport_" + DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ".xls";

Response.Clear();

Response.AddHeader("content-disposition", fileName);

Response.Charset = "";

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType = "application/excel";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

ExcelHead.RenderControl(htmlWrite);

gvOverallReport.RenderControl(htmlWrite);

string s_styleInfo = @"<style> td { mso-number-format:""" + "\\@" + @"""; }</style>";

string s_excel = stringWrite.ToString();

Response.Write(s_styleInfo + s_excel); Response.End();

The above code is written for 2003 format. But I tried changing the content type by searching in google.. .xlsx is also not working
Very small and crucial difference is there.
First case uses default type of general exception raised and throws as it is.
Second case uses a specified type of exception and here you can use this exception instance to manipulate, customize, log the exception attributes as per your requirements.
Consider example;

try
{
    dosomething()
}
catch
{
    ////Can any suggest to use this current exception other than throw
    throw;
}

try
{
    dosomething()
}
catch (Exception ex)
{
    ////Can use this current exception to display/log/customise etc. other than throw
    Console.WriteLine(ex.Message);
}

Regards
Avinash S. Godse
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 15-Mar-11 15:26pm    
I feel I need to explain my vote, probably as well as other down-votes you got.

You would be absolutely right, if not one detail: the exception in the second catch block is of the type System.Exception. If it was a different, type, your Answer would be correct, but this type will catch all thinkable exceptions, including those coming from low-level unmanaged code (in a wrapped form). So the two cases will do exact same thing, in practice.
--SA
avigodse 16-Mar-11 7:12am    
I must clear that, the question is only about the difference among two cases.
In fact i know this, and yet first case can not use the current exception instance to log or display or any other command.
And that is the only major difference among these two cases.
Pls refer improved answer.
Sergey Alexandrovich Kryukov 16-Mar-11 12:29pm    
You've modified the OP's sample to save your Answer. If you did that, you should have explained why you did and still explain that the original code is functionally the same, but it has a potential of adding of processing if you modified this and this, blah, blah... You know, half-true is not true.
--SA
avigodse 17-Mar-11 15:20pm    
Agreed, and i'll again reaffirm that question is about the differences. Both cases are same, no doubt.
I have not tried to copy but to support my answer with example.
Now you differentiate these two...
try
   {
       Console.Write("Just Test");
   }
   catch
   {
       throw new NullReferenceException();
   }



   try
   {
    // Dosomethings...
   }
   catch (NullReferenceException e)
   {
       Console.Write(e.ToString());
   }


Njoy..
 
Share this answer
 
Comments
#realJSOP 15-Mar-11 7:32am    
How is this applied as an answer to the OP's question?
Bhavna v 16-Mar-11 5:52am    
I just cleared the way how he can make sure about about his doubt... you are taking it other wise
Dalek Dave 16-Mar-11 12:47pm    
Edited for Code Block.

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