Throw statement

A

Arjen

Hi,

I'm reading the enterprise library documentation and there I see the throw
statement.
try
{
// run code
}
catch(Exception ex)
{
bool rethrow = ExceptionPolicy.HandleException(ex, "Logging Policy");
if (rethrow)
throw;
}

Can somebody tell what the throw statement does?
How can I use it? In what situation? Etc.

Thanks!
Arjen
 
J

James Jenkins

Arjen said:
Hi,

I'm reading the enterprise library documentation and there I see the throw
statement.
try
{
// run code
}
catch(Exception ex)
{
bool rethrow = ExceptionPolicy.HandleException(ex, "Logging Policy");
if (rethrow)
throw;
}

Can somebody tell what the throw statement does?
How can I use it? In what situation? Etc.

Thanks!
Arjen

It allows you to throw your own custom exceptions - for instance you may
create a class
that the Exception class. or you could pass the caught error up the stack to
display appropriate information..

James
http://www.tamarsolutions.co.uk
 
F

Fabien Bezagu

Arjen,

The throw statement, like its name says, throws an exception.

It can be used in a catch block without specifying an exception. In this
case, the exception is forwarded (using the same exception stack).

It can also be used like this :

catch (Exception e)
{
throw e;
}

However, in this case, the exception is thrown from here and a new exception
stack is created.

Fabien
 
A

Arjen

And what happens after the throw?
(the application continues? or application end?)

Arjen
 
M

Mrinal Kamboj

on doing throw it gets referred to calling layer , if it's being handled
out there , then application will continue execution , but if it gets
handled by CLR , then it will tell u about error and it's upto u to
break or continue .
 
J

Jeff Louie

If you are designing your own classes or framework and decide to use
exceptions, you need to be able to throw an exception. So if an
explicitly stated
precondition is violated by a client you can throw an exception using
the key
word throw and either one of the many existing exceptions or a custom
exception.

Now I occassionally throw an exception in the try block only to be
caught in the
catch block as a means of simplifying the internal error handling.
Purist will
frown on this.

Regards,
Jeff
 
D

David Levine

The throw statement has three general forms...
throw;
throw ex;
throw new Exception("msg",InnerException)

The differerences are subtle but important. If an exception object is
specified then the stack trace is captured and saved into the exception
object. If it is not specified then the stack trace is supposed to not be
captured and saved.

The first form is used to rethrow an existing exception, such as your code
snippet is intended to do. It is supposed to preserve the stack trace of the
original exception, but in the 1.1 framework there is a bug that actually
resets the stack trace so that catch blocks higher up the call stack will
get a stack trace that begins at the line where the throw statement occurs
rather then where the exception actually occurred. This form can ONLY be
used within the catch block itself...if you call other methods inside the
catch block then this form is not available to them (the C# compiler will
flag it as an error).

The second form rethrows the original exception but explicitly
(deliberately) resets the stack trace to start at the line of the throw
statement. Since this form does not add any useful information and
deliberately obscures the origin of the exception it has little to recommend
its use.

The third form is usually referred to as a catch-wrap-throw. The original
exception is wrapped up in a new exception object. The stack trace is
captured into the new exception object and the original exception is chained
to the new one via the inner exception so that a complete stack trace is
available...

I usually prefer the 3rd form as you can add context information to the
exception and it preserves the original exception.

The use of the throw statement is unrelated to the type of exception that is
thrown...you can throw types defined in system libraries or you can define
and throw custom exceptions.

You should also be aware that methods that you invoke inside of a catch
block can throw its own exception, which may be completely unrelated to the
original exception. In your example, the call to
ExceptionPolicy.HandleException(...) can itself call use either of the
forms:
throw ex;
throw new Exception();

Exceptions thrown within a catch block cause the remainder of the code in
the catch block to be skipped and a new exception handling process (walk the
stack looking for a catch block) will begin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top