N00b query: Why would anyone ever want to define their own Exception class?

I've been reading thru my Java textbook for the past couple hours.
Exceptions are a wonderful thing. I already found several instances where I could've implemented try/cacth in my earlier programs.
Anyway, getting back to the point. My question is, can someone give me a realistic situation/example where a custom Expcetion class is REQUIRED? (the key word here is "required"!)
I can see why someone would want to have his own Exception class..... e.getMessage() as custom error messages are SO DAMN COOL!!!!!! :P
hehe
But seriously, if you are making intermidiate/advanced Java programs, would you ever REQUIRE to make your own Exception class? Afterall, even a custom made Exception class always "extends" from a pre-defined Java class, right?
Let me make this a bit more clear... public class CustomException extends IOException{  }Now, if I am making a try/catch statement, I can simply say
try{
throw new CustomException;
catch (IOException e) { }
Now as you can see, the CustomException was caught by the catch claus, because IOException is the superclass of CustomException. So, in other words, the whole CustomException thingy didnt do anything useful.
I know I know, I am so naive. Enlighten me >.<

Sure. Say you want to have a system where you want to include a custom error code which maps to some internationalized error messages. You would create an Exception subclass with a field to hold that value separate from the normal "default" message. Then you could throw that exception in all your code. Other code can catch it as a plain Exception if they want and use the "default" message, which is okay if they don't really care about the error code.
I don't think you are ever "required" to make your own exceptions. I have done so, but I don't often. It depends. See, there are plenty of Exception subclasses in the standard packages, and most of them cover many of the things you need. So more often if I'm throwing an exception, I'll be using the already existing ones, like IllegalArgumentException or IOException (whatever is relevant to the code).
Yes, you can do what you did below with CustomException. However the reason you might do that is cuz you really want to do this:
try {
   // call some code that may throw IOException from some standard IO package
   // or may throw CustomException from some of my methods...
} catch (CustomException ce) {
   // handle cusotm exception
} catch (IOException ioe) {
   // handle IO exception
}Cuz you may want to differentiate between your exceptions vs. IOExceptions that might be thrown from some java.io class.
Usually when you use an exception class it's a named class that relates to some condition. It may hold additional information besides the standard message, but I think most of the time it's just the class name which describes the problem. And if there isn't one that describes the problem that you're code might encounter, then create a subclass.

Similar Messages

  • Why would you ever want to copy an inferior browser? I avoided updating until forced!

    I resent being forced to update to a Chrome format. I avoided it all this time until it was no longer supported and you seemed to uninstall add-ons I use. If I wanted Chrome, I would use Chrome. This almost makes me want to switch to Chrome to get the real thing, rather than your copy. What is in your head? I want the old Firefox back! I KNOW I don't like the new FF as I have to use it at work.

    Hello,
    You can use the [https://addons.mozilla.org/en-US/firefox/addon/classicthemerestorer/ Classic Theme Restorer] to bring back the old design. Learn more here: [[How to make the new Firefox look like the old Firefox]].
    Feel free to leave feedback here: https://input.mozilla.org/en-US/feedback

  • Why would anyone want to use ASM Clustered File system?

    DB Version: 11gR2
    OS : Solaris, AIX, HP-UX
    I've read about the new feature ACFS.
    http://www.oracle-base.com/articles/11g/ACFS_11gR2.php
    But why would anyone want to store database binaries in a separate Filesystem created by Oracle?

    Hi Vitamind,
    how do these binaries interact with the CPU when they want something to be done?
    ACFS should work with Local OS (Solaris) to communicate with the CPU . Isn't this kind of double work?ACFS dont work with .... but provide filesystem to Local S.O
    There may be extra work, but that's because there are more resources that a common filesystem.
    Oracle ACFS executes on operating system platforms as a native file system technology supporting native operating system file system application programming interfaces (APIs).
    ACFS is a general purpose POSIX compliant cluster file system. Being POSIX compliant, all operating system utilities we use with ext3 and other file systems can also be used with Oracle ACFS given it belongs to the same family of related standards.
    ACFS Driver Model
    An Oracle ACFS file system is installed as a dynamically loadable vendor operating system (OS) file system driver and tool set that is developed for each supported operating system platform. The driver is implemented as a Virtual File System (VFS) and processes all file and directory operations directed to a specific file system.
    It makes sense you use the ACFS if you use some of the features below:
    • Oracle RAC / RAC ONE NODE
    • Oracle ACFS Snapshots
    • Oracle ASM Dynamic Volume Manager
    • Cluster Filesystem for regular files
    ACFS Use Cases
    • Shared Oracle DB home
    • Other “file system” data
    • External tables, data loads, data extracts
    • BFILES and other data customer chooses not to store in db
    • Log files (consolidates access)
    • Test environments
    • Copy back a previous snapshot after testing
    • Backups
    • Snapshot file system for point-intime backups
    • General purpose local or cluster file system
    • Leverage ASM manageability
    Note : Oracle ACFS file systems cannot be used for an Oracle base directory or an Oracle grid infrastructure home that contains the software for Oracle Clusterware, Oracle ASM, Oracle ACFS, and Oracle ADVM components.
    Regards,
    Levi Pereira

  • HT4221 I really want my Apple TV, iPad and iPhone to sort them in date taken like in iPhoto on my iMac. How do I do that? Sorting on date modified seems so stupid to me, why would anyone need this? Date taken gives a timeline in your event.

    I really want my Apple TV, iPad and iPhone to sort them in date taken like in iPhoto on my iMac. How do I do that? Sorting on date modified seems so stupid to me, why would anyone need this? Date taken gives a timeline in your event.

    The unix commands you need are:
    GetFileInfo
    SetFileInfo
    and maybe find
    for cryptic details use the man command
    Macintosh-HD -> Applications -> Utilities -> Terminal
    man SetFileInfo
    You may use the SetFileInfo command to set the file type & the program which will open the file.
    # This little gem will do a get info on all files in a directory.
    mac $ ls  | xargs -I {} GetFileInfo "{}"
    file: "/Users/mac/playdoc/oddadocodd"
    type: ""
    creator: ""
    attributes: avbstclinmedz
    created: 05/01/2011 14:53:22
    modified: 05/01/2011 14:53:22
    file: "/Users/mac/playdoc/one.docx"
    type: ""
    creator: ""
    attributes: avbstclinmedz
    created: 05/01/2011 13:57:48
    modified: 05/01/2011 13:57:48
    file: "/Users/mac/playdoc/oneLineFile"
    type: "TEXT"
    creator: "!Rch"
    attributes: avbstclinmedz
    created: 05/07/2011 14:27:17
    modified: 05/07/2011 14:27:17
    file: "/Users/mac/playdoc/oneLineFile.txt"
    type: "TEXT"
    creator: "!Rch"
    attributes: avbstclinmedz
    created: 05/07/2011 14:27:49
    modified: 05/07/2011 14:27:49
    file: "/Users/mac/playdoc/three.docx"
    type: ""
    creator: ""
    attributes: avbstclinmedz
    created: 05/01/2011 13:58:03
    modified: 05/01/2011 13:58:03
    file: "/Users/mac/playdoc/two.docx"
    type: ""
    creator: ""
    attributes: avbstclinmedz
    created: 05/01/2011 13:57:56
    modified: 05/01/2011 13:57:56
    file: "/Users/mac/playdoc/weirder.doc.docx"
    type: ""
    creator: ""
    attributes: avbstclinmedz
    created: 05/01/2011 14:50:03
    modified: 05/01/2011 14:50:03
    # well, ! is a funnie character so we escape it.
    mac $ SetFile -t TEXT -c \!Rch two.docx
    mac $ GetFileInfo two.docx
    file: "/Users/mac/playdoc/two.docx"
    type: "TEXT"
    creator: "!Rch"
    attributes: avbstclinmedz
    created: 05/01/2011 13:57:56
    modified: 05/01/2011 13:57:56
    mac $
    mac $ date
    Sat May  7 14:40:56 EDT 2011
    mac $

  • Here is a question does anyone ever want to just kill final cut sometimes

    Here is a question does anyone ever want to just kill final cut sometimes for doing things that seriously just make no sense

    I would, but then would be out 1200 dollars.

  • Why would anyone not use VBR?

    FYI, I'm not an audio guru, and while I don't use the best audio equipment, I also don't buy the cheapest stuff out there.
    All that aside, my question to the many very knowledgeable people here is: why would someone not want to check the VBR option when encoding?

    Brenton Bills wrote:
    FYI, I'm not an audio guru, and while I don't use the best audio equipment, I also don't buy the cheapest stuff out there.
    All that aside, my question to the many very knowledgeable people here is: why would someone not want to check the VBR option when encoding?
    Because it can sometimes interfere with importing for one and not everyone can appreciate the difference.

  • Why would anyone buy these products that don't work with Win 8?

    Why would anyone buy these products that do not work with Win8?

    What are you referring to? If you have specific problems, explain them. Otherwise there's no point in posting false generalizations.
    Mylenium

  • When one or more family members who have been sharing one iTunes account wants to create their own iTunes account can they each upload the music/apps from the shared iTunes on their devices to the new iTunes account?

    When one or more family members who have been sharing one iTunes account wants to create their own iTunes account can they each upload the music/apps from the shared iTunes on their devices to the new iTunes account? 

    Yes. I would clarify your statement about the "shared" iTunes Library though. If you want each iOS device to have its own music library, you should create separate user accounts for them. Read:
    iTunes: How to share music between different accounts on a single computer
    How to use multiple iPods, iPads, or iPhones with one computer

  • Our family has one Apple id.  We would like our children to have their own I'd but keep it linked to us.  How do we do this?

    Our family has one Apple id.  We would like the kids to have their own id's but want it linked to our main account.  How do we do this?

    Family Sharing and Apple IDs for kids

  • I have been sharing my iTunes account with my kids...now they are getting older and want to have their own accounts, how do I share the songs I purchased for them on my account with them on their new accounts?

    I have been sharing my iTunes account with my kids...now they are getting older and want to have their own accounts, how do I share the songs I purchased for them on my account with them on their new accounts?  Is there a way to transer from my main account their new accounts?  And can I share music from my account with them?

    You can't currently transfer content from one account to another account. As long as the tracks are on the computer's iTunes that they use/sync to, and your account is authorised on that iTunes, then they should be able to continue to use them.

  • Why don't we  want to define equals() in Comparator interface...........

    Comparator interface has two methods: compare() & equals(). If we implement Comparator in our class(not abstract) itz unnecessary to define equals(). Why? (If we implement an interface we should define all its methods inside the class where we implement it)....

    It all hinges on what the type of Object is. Is it
    of type Comparator or the parameter you expect to be
    passed to compareTo?
    I say that Comparator makes the most sense to me.
    If you read Joshua Bloch's stuff about Comparable, he
    says it's very important to make sure that compareTo
    obeys the same contract as equals for that object.
    Why would you want to try overriding the behavior
    that might already exist in the Object that's the
    parameter for compareTo? That would be bad design,
    IMO. You might change the intentions of the person
    who wrote that class.
    %the docs say it's for comparing Comparators.
    from the docs:
    Additionally, this method can return true only if the specified Object is also a comparator and it imposes the same ordering as this comparator
    I read that to mean that the compared objects should both be Comparators (obviously) and both result in the same ordering. seems to me the method is declared on this interface to draw attention to this addition to the contract, as much as anything else

  • Anyone ever try jogging with their 30 Gig 5th Generation?

    Right now I am on the fence about getting an 8gig nano or 30 gig iPod because they are the same price. I do some jogging, and am interested if anyone ever had problems with the hard drive skipping.

    I recommend getting the 30gb 5G. I have the 60gb 5G and have jogged with it frequently. I elect to hold it in my hand while jogging for two reasons. First to reduce any risk of skipping, and second, to be able to adjust the volume easily as needed (I elect not to do volume leveling). My case keeps it isolated from the effects of perspiration.
    Despite jogging with it numerous times, it's never skipped.

  • Do Query Views use existing OLAP Cache or create their own

    Hi, I'm looking to find out if a Query View will use the Parent Query OLAP Cache or will it create it's own? 
    I am imagining that it would use the query view, but create additional cache if it drilled down on a free characteristic that wasn't in the original query output.  Any comments

    If there are any entries in the CACHE which meets the drilldown and filter criteria for the view it will be able to make use of existing Cache.
    As you mentioned for any additional drilldowns if there are not entries in the cache it will have to read from DB.

  • I have an Apple ID, and shared purchases with my 3 kids devices. Now they want to have their "own accounts" so when they get iTunes gift cards, they can pay for their own things. Can we share these purchases? My household is as follows:

    I have Mac Mini with Lion Server running 10.8.2 and an iPhone 4.
    Wife has iPad 2 and iPhone 4s. (We share Apps)
    Each kid (12 and under) has iPod Touch 4th Gen.
    So far all purchases made under my Apple ID/iTunes ID.
    Is there a way to let the kids have their own account- they get giftcards.... but if I buy an App they can still share it?
    so if Child 1 buys an App, or song, can we all enjoy it, but it's purchased from his 'own' account?

    @survivethestorm.
    Firstly thanks for the blog, I think it has saved me time contacting customer support?  I too am going thru the introduction of several kids devices, along with my wifes, onto the one iTunes account and on one PC computer (currently we have 3 x iPhones, 2 x iPod touches, 2 older iPods and soon to have iPad).  Syncing all devices hasn't been a problem to date as far as music, apps etc but I too have discovered the texting problem you described above, with my sent texts showing on the kids devices, and vice-versa, much to my horror!  It appears this is now only becoming a problem with the later iOS software and/or devices?
    It appears as you have described above, customer support has indicated by creating a separate Apple Account for each person, this fixes the texting problem?  Have there been any issues then with syncing apps, music etc after creating the accounts?  I currently dont use iCloud, I simply sync each device on connection via usb to the computer, and choose what is being synced to each device.  I like to control (at this point anyway) what is actually going on to the kids devices.  Do you know if there is a limit as to how many 'Apple Accounts' can share the one iTunes Library?  I realise the number of devices is unlimited to the one account, but just wondered if the number of Apple Accounts changes things?
    Once an Apple Account has been created for each person, do you then have to share your iTunes Library (Apps, Music etc) or does it do it automatically do so when they log in? 
    Also I presume you don't need credit card details for the Apple Account?  It was a long time ago I set my original one up.
    Sorry for the 'long winded' reply, just very confused and want to make sure I am doing the right thing.
    Many thanks.

  • My kids are getting older and they want to create their own accounts.  How do we share all the music we bought on my computer for their computers?

    We have bought music for many years on my account.  My kids are all getting older, have their own devices, but still want to access the music we bought as a family when they were younger.  How do I get their devices/accounts to all pair up within the cloud?  Is it even possible?

    You can't currently transfer content from one account to another account. As long as the tracks are on the computer's iTunes that they use/sync to, and your account is authorised on that iTunes, then they should be able to continue to use them.

Maybe you are looking for

  • Can't delete master spread

    Dear All, I'm trying to delete a master page but the option DELETE MASTER SPREAD is greyed out.  It is not in use on any page, but still won't let me delete -- any idea why? InDesign CC seems to be very quirky! Small Town Gal

  • System preferences freezes

    When i open my system preferences, it freezes. i have done a force quit, restart, shut down still freezes. When it opens all it says is loading images.

  • (urgent) Dynamic record groups and LOV

    I have to create a record group and then add 2 rows to it manually.Following is the code.There are no values displayed in the lov.Please help. PROCEDURE create_record_group IS BEGIN DECLARE rg varchar2(40) := 'test_fips'; rg_id RECORDGROUP; lov_id Lo

  • Info. required about CX_WD_GENERAL & UNCAUGHT_EXCEPTION

    Hi, Kindly let me know about exception CX_WD_GENERAL & UNCAUGHT_EXCEPTION was raised but not caught in ABAP program CX_WD_GENERAL ===CP. Please let me know about this error  & solution also. Thank ypu,

  • Invalid datetime format (null) Elements 4 and 7

    I have about 20,000 photos in Elements 4 organizer (windows XP SP2). I am trying to get the catalog in shape to be converted to Elements 7 format--it will not convert at present, telling me the file has errors in which need to be fixed before convers