Throw Statement?  
Author Message
moonwalkercs





PostPosted: Visual C# General, Throw Statement? Top

What happens if I have the following code:

try{

....

}

catch(exception ex){

throw;

}

I read that you can just call 'throw' with a parameterless catch block to rethrow the exception, but what happens if you call it with a catch block that has a parameter



Visual C#6  
 
 
James Curran





PostPosted: Visual C# General, Throw Statement? Top

The parameter on the catch statement is irrelevant. The throw; statement will just rethrow the current exception.

 
 
boban.s





PostPosted: Visual C# General, Throw Statement? Top

That is a right way of retrowing exceptions. That way is used when you want to log the exception there but also want to be catched up in the Application. If you send the exception with this code:
throw ex; then you will lose StackTrace of Exception. Just executing trow; will be the same as you don't have exception hadler in the source, and whole stack trace will be there up where you catch the exception.