473,378 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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.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
Nov 17 '05 #1
6 5693
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*****@hotmail.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(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

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

Arjen
"Fabien Bezagu" <fbezagu_at_novacor_dot_fr> schreef in bericht
news:ud**************@TK2MSFTNGP09.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*****@hotmail.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(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


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_novacor_dot_fr> schreef in bericht
news:ud**************@TK2MSFTNGP09.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*****@hotmail.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(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



Nov 17 '05 #4

"Arjen" <bo*****@hotmail.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.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
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.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.
"Arjen" <bo*****@hotmail.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.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

Nov 17 '05 #7

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

Similar topics

2
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...
2
by: Mike Schilling | last post by:
These two fragments do not act identically: (1) catch(Exception ex) { ... throw ex; } (2)
0
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...
7
by: Arjen | last post by:
Hi, I'm doing this: try { try { } catch(Exception ex){ throw;
2
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...
6
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...
13
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...
2
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) {...
4
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.