473,804 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 36330
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*******@disc ussions.microso ft.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
SomethingThatTh rowsAnException ()
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 SomethingThatTh rowsAnException 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 SomethingThatTh rowsAnException .

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*******@disc ussions.microso ft.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********@ema il.msn.com> wrote in message
news:e2******** ******@TK2MSFTN GP10.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
SomethingThatTh rowsAnException ()
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 SomethingThatTh rowsAnException 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 SomethingThatTh rowsAnException .

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*******@disc ussions.microso ft.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
6589
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 exception object that is never thrown). Thank you, Nenad
12
4329
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) { DoSomeExceptionStuff(ex); throw; }
2
2358
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 expected but I can not continue. I try start the execution and go back to running mode, but it always comes to the same line where the exception is first thrown.
1
1930
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 a program to loop sth and force outofmemory throw. however, when the w3wp exceed physical mem, the process recycle automatically, no outofmemory throw. Any idea? thank you
2
1143
by: deepak | last post by:
the code goes some thing like this try { some statements; } catch(Exception ex) { throw; }
8
17602
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 - code e0434f4d (!!! second chance !!!) So far I have been unable to create a test case that reproduces the crash. My application makes extensive use of asynchronous programming using waithandles.
4
1888
by: Rahul | last post by:
Hi Everyone, I have the following code, int main() { bool continue1 = true; while(continue1) { try
0
1181
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 using ilink32.exe. #include<stdio.h> main() { asm { mov eax, 1000h;
2
3999
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
10595
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10335
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9169
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
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
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.