473,402 Members | 2,046 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,402 software developers and data experts.

Throw Exception Vs Throw New Exception

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
New Exception?

Anq when should either be used?

Thanks,
Kerri.
Nov 22 '05 #1
3 36305
If you are not going to enclose the thrown exception inside of another
exception, you can just throw the exception that was caught.

Otherwise you can create a new exception and pass in the throw exception as
the InnerException if you wish to do so.

It is more up to how your organization is standardizing these types of
conditions, not which is the "always right" way to do it.

Throwing a new exception that is no different than the old exception just
adds one more class for the garbage collector to take care of.

Joe Feser

"Kerri" <an*******@discussions.microsoft.com> wrote in message
news:03****************************@phx.gbl...
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
New Exception?

Anq when should either be used?

Thanks,
Kerri.

Nov 22 '05 #2
Kerri,
In addition to Joe's comments, be careful with throwing an existing
exception in VB.NET. Your stack trace may be reset.

If I have:
Try
SomethingThatThrowsAnException()
Catch ex As Exception
' log the exception
Throw
End Try

When you simple use the Throw statement as I have, the stack trace will be
maintained from inside of SomethingThatThrowsAnException where the exception
really happened.

However if I had used:

Catch ex As Exception
' log the exception
Throw ex
End Try

The Stack Trace will be reset to this routine, loosing the fact that it
originally occurred inside of SomethingThatThrowsAnException.

Alternatively we can:
Catch ex As Exception
' log the exception
Throw New Exception(ex)
End Try

Which will create a new exception with a reference to the original
exception, as the inner exception.

Hope this helps
Jay

"Kerri" <an*******@discussions.microsoft.com> wrote in message
news:03****************************@phx.gbl...
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
New Exception?

Anq when should either be used?

Thanks,
Kerri.

Nov 22 '05 #3
Hey thanks for the Tip, I was not aware of that.

I always wondered why VB allowed you to do that.

No I have about 200 places to fix that code. :)

Joe

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:e2**************@TK2MSFTNGP10.phx.gbl...
Kerri,
In addition to Joe's comments, be careful with throwing an existing
exception in VB.NET. Your stack trace may be reset.

If I have:
Try
SomethingThatThrowsAnException()
Catch ex As Exception
' log the exception
Throw
End Try

When you simple use the Throw statement as I have, the stack trace will be
maintained from inside of SomethingThatThrowsAnException where the exception really happened.

However if I had used:

Catch ex As Exception
' log the exception
Throw ex
End Try

The Stack Trace will be reset to this routine, loosing the fact that it
originally occurred inside of SomethingThatThrowsAnException.

Alternatively we can:
Catch ex As Exception
' log the exception
Throw New Exception(ex)
End Try

Which will create a new exception with a reference to the original
exception, as the inner exception.

Hope this helps
Jay

"Kerri" <an*******@discussions.microsoft.com> wrote in message
news:03****************************@phx.gbl...
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
New Exception?

Anq when should either be used?

Thanks,
Kerri.


Nov 22 '05 #4

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

Similar topics

14
by: Nenad Dobrilovic | last post by:
Hi, Is it possible for exception object to be aware of it's throwing? I want to log in the text file when exeption is thrown, not when the exception object is created (because I can create...
12
by: GoogleNewsReaderMan | last post by:
I want to "rethrow" an exception so as not to lose the original stack trace information. I understood that I could use throw, like: try { DoIt(); } catch (Exception ex) {...
2
by: kpax | last post by:
Hi, While debugging my application when an explicit exception is thrown by me (or an implicit exception is thrown internally) which is not handled anywhere in the stack, the execution breaks as...
1
by: Ricky Chan | last post by:
In the production environment, it always occurs and the worker process did not recycle automatically. Therefore, it make the system service break to client. In development environment, we write...
2
by: deepak | last post by:
the code goes some thing like this try { some statements; } catch(Exception ex) { throw; }
8
by: amyl | last post by:
I have an application whose main function is encapsulated in encased in a try/catch block catching Exception. My application has crashed on several occasions with a CLR exception. CLR exception...
4
by: Rahul | last post by:
Hi Everyone, I have the following code, int main() { bool continue1 = true; while(continue1) { try
0
by: anonymous | last post by:
Hi. I am trying to perform some inline assembly language programming in C. I am currently using Windows Vista. The following code is compiled & assembled using bcc32.exe & tasm32.exe and linked...
2
by: Ryan Liu | last post by:
In C#, what's is the difference between object vs Object, exception vs Exception? Thanks, ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~. Ryan Liu Shanghai Fengpu Software Co. Ltd
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
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...

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.