473,796 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about the Exception class

So this is probably a fairly basic question, but help me out because I'm
just not lining things up and I'm somewhat new to the world of exception
handling.

What's the benefit to inheriting an exception from and of the available
parent exception classes? Does one subclass have benefits over any
other? Most of what I see involves making a new class and inheriting
from Exception so that one can have an exception class with a name of
their choosing. If you didn't care about the name would there be any
benefit to making a subclass versus raising StandardError or something
else equally vanilla? Are there any difference to library provided
exceptions other than their names?

-carl

--

Carl J. Van Arsdall
cv*********@mvi sta.com
Build and Release
MontaVista Software

Jun 14 '06 #1
2 1162
Carl J. Van Arsdall wrote:
So this is probably a fairly basic question, but help me out because I'm
just not lining things up and I'm somewhat new to the world of exception
handling.

What's the benefit to inheriting an exception from and of the available
parent exception classes? Does one subclass have benefits over any
other? Most of what I see involves making a new class and inheriting
from Exception so that one can have an exception class with a name of
their choosing. If you didn't care about the name would there be any
benefit to making a subclass versus raising StandardError or something
else equally vanilla? Are there any difference to library provided
exceptions other than their names?

-carl

If you create a new exception class that is a subclass of, say,
"ValueError ," Your exception may have some specific data that
assists you in discovering the source of the problem. Meanwhile,
any code that says:
try:
something()
except ValueError, error:
...
will catch your new exception in the ValueError clause. Subclasses
of exceptions can be seen as more specific version of the parent.

--Scott David Daniels
sc***********@a cm.org
Jun 14 '06 #2
Carl J. Van Arsdall wrote:
So this is probably a fairly basic question, but help me out because I'm
just not lining things up and I'm somewhat new to the world of exception
handling.

What's the benefit to inheriting an exception from and of the available
parent exception classes? Does one subclass have benefits over any
other? Most of what I see involves making a new class and inheriting
from Exception so that one can have an exception class with a name of
their choosing. If you didn't care about the name would there be any
benefit to making a subclass versus raising StandardError or something
else equally vanilla? Are there any difference to library provided
exceptions other than their names?


Creating your own exception hierarchy and using it consistently can help
you debugging when an exception raised by Python itself occurs. Also, you
can customize your own exception objects (such as, add new instance
attributes).

Georg
Jun 15 '06 #3

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

Similar topics

2
2044
by: dinks | last post by:
Hi, I'm new to C++ and have been assigned a task which i dont completely understand. Any help would be greately appreciated. Here is the problem: The class "linkedListType" use the "assert" facility. I am to get rid of them and replace them with exceptions. I need to create a "linkedListException" class that's declared and implemented in my "linkedListType" class. This class needs to inherit from the base "exception" class and return...
4
2796
by: Seamus M | last post by:
It seems when I use the getLine() method of the Exception class, it returns the line number of the "throw new Exception" statement and not the line number where the error actually occured in the source file. Am I doing something wrong? - Seamus
9
8494
by: Allan Ebdrup | last post by:
Hi I'm having a discussion with another programmer about a best practice for thrownig exceptions. I want to create a rich class library with many different exception classes in their own namespace, so they are easy to find with intellisense. He wants to just throw Exception or one of the build in exceptions. Do you have some good arguments for creating your own exception classes even though they are little more than a type strongname and...
4
6411
by: Divick | last post by:
Hi all, I want to subclass std::exception so as to designate the type of error that I want to throw, out of my classes, and for that I need to store the messages inside the exception classes. I want to use std::string to do that so that I don't have to deal with all the hustle of dealing with char *'s but as listed in the page (see link) below, it is not advisable to use std::string in my exception classes. The rational given is not...
4
1509
by: michael | last post by:
Hello everybody, I've created for one of my classes a exception class derivated from System.Exception. This exception class is public and lies within my class as inner class. When I throw this exception I can't catch it with its name! When I try to catch it with it's name then the catch-block will be ignored.... So I've made a general catch with System.Exception and printed the runtime type...
6
1173
by: Joel Koltner | last post by:
I have a generic (do nothing) exception class that's coded like this: class MyError(exceptions.Exception): def __init__(self,args=None): self.args = args When I attempt to raise this exception via 'raise MyError' I get an exception within the MyError constructor __init__ as follows: Traceback (most recent call last):
2
2968
by: Bob Altman | last post by:
Hi all, We have a native class modeled after the System::Exception class, and all exceptions that we throw derive from this class. For now this class is quite simple: just Description and InnerException public members. One of these days I'll dig into how to implement a StackTrace property (I assume that this is possible using something like dbghelp.dll, but I've never had the time to look into it). I've recently written a managed...
4
2654
by: amphetaman | last post by:
If I derive my own Exception class from std::runtime_error, do I have to write the destructor even if its body is empty? #include <stdexcept> class Exception : public std::runtime_error { public: explicit Exception(const std::string &description) : std::runtime_error(description) { }
1
1647
by: Tony Johansson | last post by:
Hello! If I want to create my own Exception class which class is the recommended class to derive from. Should it be from Exception or from System.ApplicationException. //Tony
0
9683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9529
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
10457
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
10013
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...
1
7550
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
6792
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
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2927
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.