473,839 Members | 1,713 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 36332
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
6592
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
2360
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
1933
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
17607
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
1182
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
4002
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
9698
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10910
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
10654
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
10297
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9426
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...
1
7833
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4066
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3136
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.