Click here to Skip to main content
14,877,073 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody
I got a question about "Error Handling" in VB.net
what's the meaning of "Throw Exception" and "Throw new Exception"?
and When we should use it?
I understand the using of Try...Catch...Finally
But I don't know the using of "Throw"?

Please help..
Posted
Updated 12-Nov-10 4:15am
v2

Throw lets you raise your own exceptions within your application which can then be caught with try..catch. You can also re-throw caught exceptions:

try
{
  myBuggyCode.myFlakyMethod("some parameter bla bla");
}
catch(Exception ex)
{
  logger.Error(ex);

  throw ex;
}
   
To expand a little on jim's answer.

throw is used to surface an Exception that has already been raised, and often caught in a catch block.

throw new ....
is often used in circumstances like:
C#
if (divisor == 0)
{
  throw new DivideByZeroException("Naughty, naughty");
}
   

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