473,770 Members | 2,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A question about Exception

Hello!!

As you know every user defined exception must be derived from class
Exception.

Now to my question if I write catch then every exception will be caught.
If I instead write catch(Exception ) then every exception will also be
caught.

These two will catch the exact same exception is this right understod?

//Tony
May 31 '06 #1
10 1507
tony wrote:
Hello!!

As you know every user defined exception must be derived from class
Exception.

Now to my question if I write catch then every exception will be caught.
If I instead write catch(Exception ) then every exception will also be
caught.

These two will catch the exact same exception is this right understod?


Yes. But on the second you need to specify a variable name to hold the
exception caught, and that is essentially the difference. If you want to do
something with the exception you caught such as log it, query its
properties, etc, then you catch (Exception ex). If you simply want to know
an exception occurred and act based on that fact alone, then you can just
catch.
--
Tom Porterfield

May 31 '06 #2

Tom Porterfield wrote:
tony wrote:
Hello!!

As you know every user defined exception must be derived from class
Exception.


Small minor correction, Microsoft recommend deriving all application
exceptions from ApplicationExce ption

May 31 '06 #3
mr*********@goo glemail.com wrote:

Small minor correction, Microsoft recommend deriving all application
exceptions from ApplicationExce ption


No they don't. Certainly not in the 2.0 framework, but even in the 1.1
framework the guideline was heavily debated.

see
http://msdn2.microsoft.com/en-US/lib...07(VS.80).aspx

--
Willem van Rumpt
May 31 '06 #4
mr*********@goo glemail.com wrote:

Small minor correction, Microsoft recommend deriving all application
exceptions from ApplicationExce ption


Not really, not anymore. The following article is a good place to start
reading about exceptions and how to handle them:

Best Practices for Handling Exceptions
http://msdn2.microsoft.com/en-US/library/seyhszts.aspx

To address your specific statement, you'll find the following:

"For most applications, derive custom exceptions from the Exception class.
It was originally thought that custom exceptions should derive from the
ApplicationExce ption class; however in practice this has not been found to
add significant value."
--
Tom Porterfield

May 31 '06 #5
There is a small addendum that should be said about catch and
catch(Exception e).

The CLR actually allows any object type to be thrown as an exception,
not just types derived from Exception. C#, however, will not allow you to
generate code that does this.

However, you can certainly use a type in a referenced assembly generated
in IL or C++ which will throw an object derived from a class other than
exception.

In this case, your catch(Exception e) clause will not catch that.

However, your catch statement will.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Tom Porterfield" <tp******@mvps. org> wrote in message
news:uH******** ******@TK2MSFTN GP04.phx.gbl...
tony wrote:
Hello!!

As you know every user defined exception must be derived from class
Exception.

Now to my question if I write catch then every exception will be caught.
If I instead write catch(Exception ) then every exception will also be
caught.

These two will catch the exact same exception is this right understod?


Yes. But on the second you need to specify a variable name to hold the
exception caught, and that is essentially the difference. If you want to
do something with the exception you caught such as log it, query its
properties, etc, then you catch (Exception ex). If you simply want to
know an exception occurred and act based on that fact alone, then you can
just catch.
--
Tom Porterfield

May 31 '06 #6
Nicholas Paldino [.NET/C# MVP] wrote:
There is a small addendum that should be said about catch and
catch(Exception e).

The CLR actually allows any object type to be thrown as an exception,
not just types derived from Exception. C#, however, will not allow you to
generate code that does this.

However, you can certainly use a type in a referenced assembly
generated in IL or C++ which will throw an object derived from a class
other than exception.

In this case, your catch(Exception e) clause will not catch that.

However, your catch statement will.

Hope this helps.


It does. In such an instance as this is there any way to get additional
information about what was thrown?

Seems like bad practice to me, to throw something other than an exception.
Any known places that do this (ie. within the framework or in other commonly
used libraries)?
--
Tom Porterfield

May 31 '06 #7
Tom,

AFAIK, there are no known places that actually throws something other
than exception in the framework library (thankfully, I might add).

And no, in C#, there is no way to access the information. However, in
C++, I believe you can access the object (you would have to cast it, of
course, to get any meaningful information).

Generally, it's something that I would not code against in C#. Perhaps
if I needed to handle something that a third-party component was doing, but
in that case, I would have serious reservations about continuing the use of
that component.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Tom Porterfield" <tp******@mvps. org> wrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
Nicholas Paldino [.NET/C# MVP] wrote:
There is a small addendum that should be said about catch and
catch(Exception e).

The CLR actually allows any object type to be thrown as an exception,
not just types derived from Exception. C#, however, will not allow you
to
generate code that does this.

However, you can certainly use a type in a referenced assembly
generated in IL or C++ which will throw an object derived from a class
other than exception.

In this case, your catch(Exception e) clause will not catch that.

However, your catch statement will.

Hope this helps.


It does. In such an instance as this is there any way to get additional
information about what was thrown?

Seems like bad practice to me, to throw something other than an exception.
Any known places that do this (ie. within the framework or in other
commonly used libraries)?
--
Tom Porterfield

May 31 '06 #8
Nicholas Paldino [.NET/C# MVP] wrote:

AFAIK, there are no known places that actually throws something other
than exception in the framework library (thankfully, I might add).

....

Thanks.
--
Tom Porterfield
May 31 '06 #9
Willem van Rumpt wrote:
mr*********@goo glemail.com wrote:

Small minor correction, Microsoft recommend deriving all application
exceptions from ApplicationExce ption


No they don't. Certainly not in the 2.0 framework, but even in the 1.1
framework the guideline was heavily debated.

see
http://msdn2.microsoft.com/en-US/lib...07(VS.80).aspx


Understanding Exceptions and Exception Hierarchies
http://msdn.microsoft.com/practices/...ceptdotnet.asp

"All exception classes should derive from the Exception base class
within the System namespace for compliance with Common Language
Specification (CLS)"

That's my source

Jun 1 '06 #10

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

Similar topics

6
1798
by: Flare | last post by:
Hi i just read: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/exceptdotnet.asp Wich is interesting reading by the way. But. I have'nt used exception very much to anything else than ordinary FileExeptions etc. Now i want to create my own exception hierarchy. I want to do this so that i can log the exceltion information to a file or a database. Doesent mather right now.
9
2538
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in the way of CPU usage), compared to the "normal" practise returning values. How true is this? Will using using exception handling, in general, be much less efficient than returning values, or less efficient at all? Just curious...
5
1951
by: John Richardson | last post by:
Quick question about the UnhandledException event and associated Handler. I just implemented this handler for the first time, and am surprised that it this event is being raised for an exception that I have handled. I have the following: private void Load() { try {
9
2229
by: Gustaf | last post by:
I'm confused about structured error handling. The following piece of code is a simplification of a class library I'm working on. It works, and it does what I want, but I'm still not convinced that I have been doing it right. I think I overdo it. Please have a look: -- using System; using System.IO;
2
1500
by: Shi Mu | last post by:
very hard for me to understand the difference between try...except and try...finally
4
1854
by: Steve | last post by:
I have read a couple articles online, read my Jesse Liberty book but I am still confused as to just what the best practices are for using exceptions. I keep changing how I'm working with them and it has now, after 15k lines of code resulted in a royal mess! It's my hope to ask some specific questions with scenario examples and that some of you might offer a little guidance or general suggestions. 1) string...
1
1377
by: UJ | last post by:
I am doing development on a machine and everything was working fine. The name of the project was ECS to I made all my references as ~/ECS/... Worked great. Put it on the final server running 2003/IIs 6and it bitched it couldn't find the directory. I created a virtual directory, it complained because web.config wasn't there. I removed the ECS from all the references and it works great on both my devo and the final machine.
15
1900
by: =?Utf-8?B?TWljaGVsIFBvc3NldGggW01DUF0=?= | last post by:
In my opinion rethrowing exceptions without providing anny extra information is a totall waste Examples : in my opinion wrong : A: Public sub DoSomeStuff() Try do it
6
1526
by: rhaazy | last post by:
I am looking for some feedback on using try catch statements. Usually when I start a project I use them for everything, but stop using them as often after the "meat n' potatos" of the project is finished. I am thinking it would be useful to at least have a blanket try-catch to surround all of the code, and add more to aid in debugging and catching specific exceptions. I want to change my habits but before I do I wanted to see if anyone...
0
9425
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
10228
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...
0
9869
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
8883
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
7415
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
6676
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
5312
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...
1
3970
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
3575
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.