473,549 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Throw statement

Hi,

I'm reading the enterprise library documentation and there I see the throw
statement.
try
{
// run code
}
catch(Exception ex)
{
bool rethrow = ExceptionPolicy .HandleExceptio n(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
Nov 17 '05 #1
6 5708
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

"Arjen" <bo*****@hotmai l.com> a écrit dans le message de news:
dc**********@ne ws6.zwoll1.ov.h ome.nl...
Hi,

I'm reading the enterprise library documentation and there I see the throw
statement.
try
{
// run code
}
catch(Exception ex)
{
bool rethrow = ExceptionPolicy .HandleExceptio n(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

Nov 17 '05 #2
And what happens after the throw?
(the application continues? or application end?)

Arjen
"Fabien Bezagu" <fbezagu_at_nov acor_dot_fr> schreef in bericht
news:ud******** ******@TK2MSFTN GP09.phx.gbl...
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

"Arjen" <bo*****@hotmai l.com> a écrit dans le message de news:
dc**********@ne ws6.zwoll1.ov.h ome.nl...
Hi,

I'm reading the enterprise library documentation and there I see the
throw statement.
try
{
// run code
}
catch(Exception ex)
{
bool rethrow = ExceptionPolicy .HandleExceptio n(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


Nov 17 '05 #3
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 .

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

Arjen
"Fabien Bezagu" <fbezagu_at_nov acor_dot_fr> schreef in bericht
news:ud******** ******@TK2MSFTN GP09.phx.gbl...
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

"Arjen" <bo*****@hotmai l.com> a écrit dans le message de news:
dc**********@ news6.zwoll1.ov .home.nl...
Hi,

I'm reading the enterprise library documentation and there I see the
throw statement.
try
{
// run code
}
catch(Except ion ex)
{
bool rethrow = ExceptionPolicy .HandleExceptio n(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



Nov 17 '05 #4

"Arjen" <bo*****@hotmai l.com> wrote in message
news:dc******** **@news6.zwoll1 .ov.home.nl...
Hi,

I'm reading the enterprise library documentation and there I see the throw
statement.
try
{
// run code
}
catch(Exception ex)
{
bool rethrow = ExceptionPolicy .HandleExceptio n(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
Nov 17 '05 #5
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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #6
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 .HandleExceptio n(...) 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.
"Arjen" <bo*****@hotmai l.com> wrote in message
news:dc******** **@news6.zwoll1 .ov.home.nl...
Hi,

I'm reading the enterprise library documentation and there I see the throw
statement.
try
{
// run code
}
catch(Exception ex)
{
bool rethrow = ExceptionPolicy .HandleExceptio n(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

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1746
by: Ben | last post by:
I have been experiencing which I've managed to simplify down to the following code. Is my assertion in the code misplaced? My compiler (MSVC++ 6) hits the assert when I believe it should just abort after the second throw. #include <windows.h> #include <assert.h> void anotherFunction() {
2
2928
by: Mike Schilling | last post by:
These two fragments do not act identically: (1) catch(Exception ex) { ... throw ex; } (2)
0
1142
by: Lasse Vågsæther Karlsen | last post by:
From the book by Jeffrey Richter: throw; will not change the origin of the exception, whereas throw ex; will change the origin of the exception to this statement. When I try the following program, I get this result: 3: at ... in c:\dev\vs.net\exceptiontest3\form1.cs:line 91 2: at ... in c:\dev\vs.net\exceptiontest3\form1.cs:line 101 1:...
7
10175
by: Arjen | last post by:
Hi, I'm doing this: try { try { } catch(Exception ex){ throw;
2
3064
by: Kerri | last post by:
Hi, I am new to .NET In my Error Logic on my Aspx pages when an error happens it hits my catch statement where I throw an Exception. My question is : what is the difference between Thwo Exception and Throw
6
1906
by: Jeff Johnson [MVP: VB] | last post by:
My testing seems to indicate that it won't, but no documentation I can find explicitly states this. Let's say I throw a customer exception like this: Try m = New MyObject Catch ex As Exception Throw New MyObjectCreationFailedException(<stuff>) LaunchNuclearMissiles() End Try
13
2046
by: Jacek Dziedzic | last post by:
Hi! <OT, background> I am in a situation where I use two compilers from different vendors to compile my program. It seems that recently, due to a misconfiguration, library conflict or my ignorance, with one of the compilers I am having trouble related to libuwind.so, which, to my knowledge, deals with the intricacies of unwinding the...
2
1652
by: Bit byte | last post by:
I have a simplistic exception class like so: class CommException { public: CommException(const char *err){ strncpy(msg,err,ERR_MSG_LEN); } virtual ~CommException(){;} void erase(void) { memset(msg,'\\0',ERR_MSG_LEN); } const char* report(void) { return msg ; } private:
4
6463
by: Henning Makholm | last post by:
Having started in a job where I am to write Java code, I am working my way through the Java Language Specification. But the following situation gives me problems: public class Foo { public static void foo(boolean b) { try { System.out.println(b); if( b ) throw new Exception(); } finally { return ; }
0
7723
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7965
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7817
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6051
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3487
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
771
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.