Better idiom for a factory?

Our project has about 25 exception classes, which are all subclasses of a common exception class (call it FooException) which itself extends java.lang.Exception with some additional functionality to make these exceptions easier to use in our framework.
Until now I had been calling the constructors for each class explicitly, e.g.
throw new NoCoffeeException("out of coffee", t, x);and there were several constructor signatures, depending on the context and the information that needed to be conveyed.
I had most of the code factored out to the FooException superclass, but each subclass still needed each constructor explicitly written, even though they were identical except for the class name.
Now that I need to add about 3 more constructor signatures, I decided this was nuts and am rewriting this to use a factory instead. It's pretty much working as I want except I'm not completely sure of the best way to indicate the desired class to the factory.
What I currently have is:throw FooException.make(NoCoffeeException.class, "out of coffee", t, x);which works fine except I'm not completely thrilled about clients having to say "NoCoffeeException.class". No good reason but it just seems a little ugly and "feels" like it breaks encapsulation.
But I can't think of a better way to indicate it, other than introducing another set of constants, which seems entirely redundant and just as bad.
How does this sit with you? Does this seem natural? Thanks...

Just an off the wall idea (and I have NEVER seen this
before) - a simple constructor with setX() methods?
NoCoffeeException nce = new NoCoffeeException();
nce.setMessage( "Bah!" );
nce.setLocalization( whateverGoesHere );
nce.setThrowable( t );
throw nce;like I said, I've never seen that done with
exceptions, but if you have an IDE with the "right
click->add get/set methods" feature its much faster...Throwable, in J2SE 1.4, has an initCause(Throwable rootCause) method that allows you to chain exceptions, and is usually used just as your setX() methods are. This is often a good way to do it.
As syntactic sugar, initCause() returns 'this', as Throwable, to support method invocation chaining. (Invocation chaining is also all over the java.nio package. See java.nio.Buffer.position() and others for example.) So it could look like this:
try {
  something();
} catch (IOException e) {
  CoffeeException ce = new CoffeeException("Network error");
  ce.initCause(e);
  throw ce;
}or, with invocation chaining:
try {
  something();
} catch (IOException e) {
  throw (CoffeeException) new CoffeeException("Network error").initCause(e);
}Unfortunately (as with java.nio.Buffer) most of the time, you must downcast to the proper type as the superclass method is declared to return Throwable. If only java had covariant return types! Hopefully in Tiger (J2SE 1.5).
See http://developer.java.sun.com/developer/bugParade/bugs/4144488.html for more on covariant return types in Java.
Cheers,
Colin

Similar Messages

  • Updated | BlackBerry Beta Zone for BlackBerry 10 | v10.0.0.22

    Load your BlackBerry 10 Beta applications through our brand new native app.
    Available Features:
    • Create a Beta Zone profile (for new users)
    • Automatically links your BlackBerry ID to your Beta Zone profile (for existing users)
    • Join new beta programs and download beta applications right from the app!
    In order to use this application to access BlackBerry Beta Zone, you must be a registered member of BlackBerry Beta Zone. If you are not currently a member, you will be prompted to enroll for a Beta Zone user account.
    New features in this version include:
    Feedback Form: submit your bug reports and feature suggestions right from the app
    Device info capture: keeping your Beta Zone profile up-to-date has never been easier! Includes handset model, PIN and OS version.
    UI and performance improvements
    Please note that the old version of the app will be disabled on October 10th, so do grab this update as soon as you can.
    Download BlackBerry Beta Zone for BlackBerry 10
    VIA | Blackberry World
    http://appworld.blackberry.com/webstore/content/35142896
    Please thank those who help you by clicking the button.
    If your issue has been solved, please resolve it by marking "Accept as Solution"

    You cannot downgrade.
    You can reset your phone to factory settings if you wish to see if it corrects your issue. Resetting to factory settings will wipe out all your data so you should save it first. To save all your stuff download and install BlackBerry Link on your computer from the link below. Resetting will not back-up the OS on your phone. Once you have downloaded and installed BlackBerry Link connect your phone to your computer and save your stuff. Once saved disconnect your phone from the computer.
    To reset your phone: tap the Settings App, scroll down and tap Security and Privacy, tap Security Wipe, tap the blank box and enter blackberry.
    Once Wiped reconnect your phone to your computer and install your stuff.
    http://us.blackberry.com/software/desktop/blackberry-link.html
    http://ca.blackberry.com/software/desktop/blackberry-link.html?LID=ca:bb:software:desktopsoftwarec&L...

  • Is there a better OS for iPad?

    I have been very glad to have my iPad but now it has lost all his right to life.
    it is TOTALLY useless to me and the source of daily, no, hourly serious nuistance.
    Is there a better OS for iPads to *work* with? (iOS 8 seems to be just a public nuisance.) I got from the people there nice hints to clear something, some resets I performed though I am not interested to delete all the content.
    How to get a system that can cut or copy text or images und paste it anywhere? A glourios matter for MacOS 1.01

    Karl Pfeiffer (.at) wrote:
    I am not interested to delete all the content.
    If you want an iPad that is fresh and clean, just like when it came from the factory, you have no choice ... you HAVE TO DELETE all the content. Of course you'll want to do backups before you do that, and you'll need to understand what is saved on your backup and what is not saved.
    Let me know if you want to go over the details on how to get your iPad back to factory default state, just like brand new.

  • Beta drivers for Audigy2

    Well, the title says what I'm looking for, but here's my problem:
    I had installed Quake4, and was playing fine. After I had finished the game, I went to update my drivers (finally,) and noticed that I was running my NX on generic windows drivers. I updated all the drivers, mediasource, etc... (along with some other housekeeping updates,) and I now can no longer open the game.
    I've already eliminated the video card update as the problem, and I honestly don't know of a way to eliminate a bios update issue, however, I HIGHLY doubt that is the culprit. After some discussions in the quake disc. boards, I sence that there is a problem with the creative drivers. I have been told that most people are getting success by using the latest beta drivers, and I was linked to a file called "SBA2_PCDrvBeta_LB_2_08_0002.exe" However, when I try to run that file, I'm told that "Setup is unable to detect a supported device on your system..." As the title says Audigy2/ZS, Audigy 4, I'm thinking these are the wrong beta drivers for my device, but I can't find beta drivers for the Audigy2 NX USB
    Help? Thanks...

    I have tried re-installing, that isn't working. I'm also hearing that people are having success when installing the OpenAL libs....
    I've been trying to get this file: http://developer.creative.com/articl...&top=38&aid=46 however, whenever I try to d/l the file I time-out to a "page unavail." error... Is there some server error at creative? ...or, does anyone know of a mirror server?
    The file I'm looking for is: OpenALwEAX.exe
    Message Edited by Toker on -02-2005 0:45 PM

  • How can I get a better driver for my 1394b Hostcontroller in my Macbook pro 2010 to solve the problem, that it is too slow under Windows 7 installed with bootcamp ?

    How can I get a better driver for my 1394b Hostcontroller in my Macbook pro 2010 to solve the problem, that it is too slow under Windows 7 installed with bootcamp ?
    WIndows can't find a better one than the installed LSI Conformed 1394 Hostcontroller

    Read and follow the Bootcamp Installation instructions that you were offered to view when you ran Bootcamp Assistant.
    Run Bootcamp Assistant again and select the option to download the Bootcamp Support software. Follow the instructions. Then boot into Windows and install the Windows Support software you downloaded and saved.

  • Beta driver for X-Fi (SBXF_W7DRVBETA_US_2_18_0008) users feedback thread

    <div class="DownloadDate">Release date : 28 Jan 09<div class="DownloadShortDesc">This?download is a beta?driver providing Microsoft? Windows?7?Beta support for Creative Sound Blaster? X-Fi?series of audio devices. For more details, read the rest of this web release note.<div class="DownloadDetailDesc"><div class="DownloadFileName">File Name : SBXF_W7DRVBETA_US_2_8_0008.exe<div class="DownloadText">This?download is a beta?driver providing Microsoft? Windows?7?Beta support for Creative Sound Blaster? X-Fi?series of audio devices. For more details, read the rest of this web release note.
    Take note of the following:
    THIS IS AN UNSUPPORTED BETA DRIVER. There is no technical support for this driver.<
    We recommend that only experienced users install this driver. Do not install this driver on a system used to perform critical tasks.<
    Users who are looking for stable drivers should wait for the final release. When this happens, user should install the latest Sound Blaster X-Fi?series?driver available.<
    This download supports the following audio devices only:
    Sound Blaster X-Fi XtremeMusic<
    Sound Blaster X-Fi XtremeGamer?Fatalty? Professional Series<
    Sound Blaster X-Fi XtremeGamer<
    Sound Blaster X-Fi Platinum?Fatalty Champion Series?<
    Sound Blaster X-Fi Platinum<
    Sound Blaster X-Fi Fatalty<
    Sound Blaster X-Fi Elite Pro<
    Sound Blaster X-Fi<
    Requirements:
    Microsoft Windows?7 Beta<
    Creative Sound Blaster X-Fi?series audio devices listed above<
    Notes:
    To install this?driver
    Download the?.exe file onto your local hard disk.<
    Double-click?the?downloaded file.<
    Follow the instructions on the screen.<
    <?
    http://support.creative.com/download...verType=#type_
    Does anyone have any experiences installing this over Sound Blaster X-Fi series Support Pack .0

    0Re: New driver? SBXF_W7DRVBETA_US_2_8_0008.exep
    gazzamcg wrote:
    robertmc8 wrote:
    Does CMSS work with this new driver? I don't have win 7 on my computer anymore hehe. Does support 8channel Ditigal out? Just wondering last time it was only 2channel support when I look at it. I can't go back windows 7 I'm going send my Visiontek 2600 Pro AGP 52 card in for replacement since x700pro doesn't support win7Message Edited by robertmc8 on 0-28-2009 03:5 PM
    Yes, CMSS work's with this beta. I don't know if the 7. digital out works ....
    The sound is excellent !
    kpo6969, why would you wan't to install over existing drivers ?
    Better to uninstall existing drivers first, then install the beta's as this?should avoid any conflicts.
    - - - - - - - - - Yellow matter custard, dripping from a dead dog's eye. Crabalocker fishwife, pornographic priestess,Boy, you been a naughty girl you let your knickers down. I am the eggman, they are the eggmen. I am the walrus, goo goo g'joob ?- - - - - - - - - - - - -
    Message Edited by gazzamcg on 0-28-2009 0:26 PM
    Ok sorry my mistake misspell digital word blame on my speach issue when I was growing up as a kid lol.
    Anyway Can u check digital In l in for me thanks Last time I check it only support 2channal I don't want formated back to windows 7 I can't anyway to I get my HD 2600pro card back going send in get fix so that mean I be on my x700pro card for next couple weeks.
    I won't do driver mod yet to I know whats it's doing later on hehe.

  • I bought the photoshop install online and I put it on my laptop. But now I have a new and better computer for photo and video but I can not figure out how to reinstall my photoshop on this new computer. When I bought it, they said I would be able to insta

    I bought the photoshop install online and I put it on my laptop. But now I have a new and better computer for photo and video but I can not figure out how to reinstall my photoshop on this new computer. When I bought it, they said I would be able to install it on 2 different computers. PS they are both PC computers.

    Downloadable installation files available:
    Suites and Programs:  CC 2014 | CC | CS6 | CS5.5 | CS5 | CS4, CS4 Web Standard | CS3
    Acrobat:  XI, X | 9,8 | 9 standard
    Premiere Elements:  13 |12 | 11, 10 | 9, 8, 7
    Photoshop Elements:  13 |12 | 11, 10 | 9,8,7
    Lightroom:  5.7.1| 5 | 4 | 3
    Captivate:  8 | 7 | 6 | 5
    Contribute:  CS5 | CS4, CS3
    Download and installation help for Adobe links
    Download and installation help for Prodesigntools links are listed on most linked pages.  They are critical; especially steps 1, 2 and 3.  If you click a link that does not have those steps listed, open a second window using the Lightroom 3 link to see those 'Important Instructions'.window using the Lightroom 3 link to see those 'Important Instructions'.

  • Which is better software for brochures and PDF forms ? Photoshop or InDesign ?

    Which is better software for brochures and PDF forms ? Photoshop or InDesign ? and why ?

    If you are going to be making a lot of brochures, with photos, you probably want both. Photoshop to edit the photos, and InDesign to assemble and layout images with text.

  • IPad constantly glitching/freezing/crashing/parts of screen unresponsive - time for a factory reset OR a trip to the Apple store???

    iPad constantly glitching/freezing/crashing/parts of screen unresponsive - time for a factory reset OR a trip to the Apple store???
    My son has nothing but problems with his iPad Mini.  It is less than a year old and it constantly freezes/crashes/glitches and parts of the screen are unresponsive.  This happens while playing games, watching movies, surfing the net etc etc
    The iPad has been in a heavy duty Survivor case from day 1 and has been very well taken care of - so I don't think it's a damage issue
    I have tried numerous resets (just holding the 2 buttons) and have disabled every feature on the iPad that might be causing memory/RAM issues (?)  all to no avail.
    I haven't tried resetting to factory settings as I don't want to lose all the data on the iPad (mostly his game achievements and Minecraft maps that he has worked so hard on!!!)
    SO - any suggestions:
    1) Take it in to the Apple Store - I have never gone to Apple over an issue, is this possibly covered by warranty???
    2) Factory Reset - If I do this can I save all his game achievements/data???
    Cheers,
    Todd

    Try this  - Reset the iPad by holding down on the Sleep and Home buttons at the same time for about 10-15 seconds until the Apple Logo appears - ignore the red slider - let go of the buttons. (This is equivalent to rebooting your computer.) No data/files will be erased. http://support.apple.com/kb/ht1430
    Frozen or unresponsive iPad
    Resolve these most common issues:
        •    Display remains black or blank
        •    Touch screen not responding
        •    Application unexpectedly closes or freezes
    http://www.apple.com/support/ipad/assistant/ipad/
    iPhone, iPad, iPod Touch: Troubleshooting Touchscreen Response
    http://support.apple.com/kb/ts1827
    iPad Frozen, not responding, how to fix
    http://appletoolbox.com/2012/07/ipad-frozen-not-responding-how-to-fix/
    iPad Frozen? How to Force Quit an App, Reset or Restart Your iPad
    http://ipadacademy.com/2010/11/ipad-frozen-how-to-force-quit-an-app-reset-or-res tart-your-ipad
    Black or Blank Screen on iPad or iPhone
    http://appletoolbox.com/2012/10/black-or-blank-screen-on-ipad-or-iphone/
    What to Do When Your iPad Won't Turn On
    http://ipad.about.com/od/iPad_Troubleshooting/ss/What-To-Do-When-Your-Ipad-Wo-No t-Turn-On.htm
    iOS: Not responding or does not turn on
    http://support.apple.com/kb/TS3281
    iPad: Basic troubleshooting
    http://support.apple.com/kb/TS3274
    Not normal. Take it to an Apple Store for evaluation. You're covered by Apple's 1-year warranty.
    Make a Genius Bar Reservation
    http://www.apple.com/retail/geniusbar/
     Cheers, Tom

  • I was an apple beta tester for iOS7 and now my iPhone 5 shut down, it's unresponsive it gets recognized by iTunes when plugged into the computer but that's it. Since I was a beta tester is there any way to fix my iPhone??

    I was an apple beta tester for iOS7 and now my iPhone 5 shut down, it's unresponsive it gets recognized by iTunes when plugged into the computer but that's it. Since I was a beta tester is there any way to fix my iPhone??

    Are you still running iOS 7 bet?. If so go to the private developer section for help. We cannot help you here with beta software.

  • Call upon even better support for Unicode

    Hello
    Following some messages I have posted regarding problems I encountered while developing a non-English web application, I would like to call upon an even better support for Unicode. Before I describe my call, I want to say that I consider Berkeley DBXML a superb product. Superb. It lets us develop very clean and maintainable applications. Maintainability is, in my view, the keyword in good software development practices.
    In this message I would like to remind you that the US-ASCII 8-bit set of characters only represents 0.4% of all characters in the world. It is also true to say that most of our software comes from efforts of American developers, for which I am of course very grateful.
    But problems with non US-ASCII characters are very very time consuming to solve. To start with, our operating systems need to be configured especially for unicode, our servers too, our development tools too, our source code too and, finally, our data too. That's a lot of configuring, isn't it? Believe me, as a Flemish french-speaking, danish-speaking developer who develops currently a new application in Portuguese, I know what I am talking about.
    Have you ever tried to write a Java class called Ação.java, that loads an xml instance called Ação.xml that contains something like <?xml version="1.0" charset="utf-8"?></ação variável="descrição"/>? It takes at least the double of time to have all this work right in a web application on a Linux server than it would take to write a Acao.java that loads Acao.xml containing <?xml version="1.0" charset="us-ascii"?></acao variavel="descricao"/> (which is clearly something we do not want in Portugal).
    I have experienced a problem while using the dbxml shell to load documents that have utf-8 encoded names. See difficulties retrieving documents with non ascii characters in name The work around is not to use the dbxml shell, with which I am of course not very happy.
    So, while trying not to be arrogant and while trying to express my very very great appreciation for this great product, I call upon even better support for Unicode. After all, when the rest of us, that use another 65279 characters in our software, will be able to use this great product without problems, will it not contribute to the success of Berkeley DBXML?
    Thank you
    Koen
    Edited by: koenheene on 29/Out/2009 3:09

    Hello John and thank you for replying,
    You are completely correct that it is a shell problem. I investigated and found solutions for running dbxml in a Linux shell. On Windows, as one could expect, no solution so far.
    Here is an overview of my investigation, which I hope will be useful for other developers which also presist writing code and xml in their language.
    difficulties retrieving documents with non ascii characters in name
    I was wondering though if it would not be possible to write the dbxml shell in such a way that it becomes independent from the encoding of the shell. Surely their must be, not? Rewrite dbxml in Java? Any candidates :-) ?
    Thanks again for the very good work,
    Koen

  • Beta available for older Mac's ?

    I see there is a Safari for those who have Windows, but those who have the older Macs are unable to update to the newest Safari?
    Why is this?
    And is there a free beta release for those with an older mac ?

    Sad that this is the case, and it doesn't explain the "why" of it... but perhaps it is like many things in life, where we have to wait until we get to Heaven to understand the difficult questions in life. haha
    Anyhow... I suppose I will live... or just use Firefox.
    Have a wonderful week!

  • What is better way for creating a thread? Why?

    A thread can be create in two way, extending Thread class or implimenting Runnable.
    What is better way for creating a thread? Why?

    Implementing Runnable
    See FAQ #1 here [http://forum.java.sun.com/ann.jspa?annID=9]

  • Is After Effects a better option for Chroma???

    Hi all,
    For some time I have not been happy with the quality of our finished chroma key output. I am using a green screen, professional lighting and camera operator, as well as Color Smoother, 8 Point Garbage Matte etc etc
    In a previous life I used After Effects with its KEYLIGHT plug in - which seems to be one pretty powerful Chroma Keyer. While I am happy enough in FCP - I just don't feel MOTION comes near AE.
    I am committed to FCP permanently but was thinking of going back to AE over MOTION as a lot of our work is green screen based.
    Any thing I am missing before I blow the cash on AE?
    Thanks all,
    JD

    Absolutely. If you're getting AE from a bundle and you need Photoshop and Illustrator then you'll get a good product with AE, but it should not be the goal. Shake is now just a cheap and still one of the best compositors out there. Bare in mind that Jackson used Shake to composite Lord of the Rings, and King Kong. Nobody can say that was an OK job, if only they had a better program for the compositing. You will have to learn a new interface type. AE, Final Cut, Motion, and Photoshop are all layer based apps, and shake is node based, much like Color. It's a great way to work once you get it, so spend the time to really familiarize yourself with the style. I highly recommend Shake.

  • My LG Optimus Zone is having software issues and I can't access the Recovery Menu for a Factory Reset

    My LG Optimus Zone is having software issues and I can't access the Recovery Menu for a Factory Reset. It was working fine until I went to activate it and update the PRL. It turns on, but Freezes on the LG Logo. it currently goes into the LG Download Mode, and When I restore with LG's Mobile Support Tool, It finishes, reboots, and stays at the boot Logo. I was wondering if I can possibly get a replacement phone? This is a little upsetting, as i was hoping to have it transfered from prepaid to postpaid service next month

    Who is your Internet Service Provider...?
    Do you have any computer which is able to go online wirelessly...?
    Have you forwarded any ports on the router...?
    Try to adjust the wireless settings on the router and check...
    Open an Internet Explorer browser page on your wired computer(desktop).In the address bar type - 192.168.1.1 and press Enter...Leave Username blank & in Password use admin in lower case...
    Under the Wireless tab,Change the SSIS(Network Name) to some unique name.Change the Channel Width to 20MHz only and Channel to 11,click on save settings.Under the Wireless Security subtab,Change the Security to WEP and type any ten digit number in key1 as the network security key,click on save settings..Under the Advanced Wireless Settings,Change the Beacon Interval to 75,Change the Fragmentation Threshold to 2304,Change the RTS Threshold to 2304 and Click on Save Settings...
    Also,for the gaming system,check this link.

Maybe you are looking for