Is it okay practice to have a constructor throw an exception?

Or is it just plain bad practice and it's better to catch it instead.

I don't know if there is anything wrong with the/a
constructor throwing an exception. It is a way to
inform the user that something "bad" occurred during
creation of the object.
I usually don't do it. I have my constructor call a
method that encapsulates the code that may generate
an error.
public class MyExample
public MyExample()
try
aMethodThatPerformsIO();
catch(IOException ioe)
n ioe) {ioe.printStackTrace();}
private void aMethodThatPerformsIO() throws
ows IOException
//blah
Presumably aMethodThatPerformsIO initializes the state of the MyExample being constructed, yes? That's why you call it in a ctor, yes?
MyExample myEx = new MyExample(); // exception is thrown, stderr gets some bytes, object is not properly initialized, BUT...
myEx.doSomethingThatAssumesValidState(); // ...this still executes (or tries to) despite the assumption about valid state being violated like godlie at a biker bar.I'd rather have the ctor throw the exception, but that's JMAO.

Similar Messages

  • Can a static constructor throw an exception?

    Or is it not possible?

    If you throw a checked exception from an initializer
    it won't even compile. There's no trying about it.From a static initializer. A non-static initializer, via the spec, is supposed to allow this. Although Sun VMs previous to 1.4 did not allow it due to a bug....
    http://forum.java.sun.com/thread.jsp?forum=4&thread=198991

  • Throw difference exception in constructor

    Have a base class constructor that throws an exception of class XException
    Have an exception class YException that inherit exception XException
    If I inherit the base class, can I make it throw exception Y instead of X, since Y is a subclass of exception X?
    Example:
    public class Base {
    public Base() throws XException {
    public class XException extends Exception {
    public XException() {
    // etc...
    public YException extends XException {
    YException() {
    super();
    public MyClass extends Base {
    // Can this work in a constructor since YException is a subclass of XException?
    // Anyway to make this possible?
    public MyClass() throws YException {
    super();

    Have a base class constructor that throws an exception
    of class XException
    Have an exception class YException that inherit
    exception XException
    If I inherit the base class, can I make it throw
    exception Y instead of X, since Y is a subclass of
    exception X?
    Example:
    public class Base {
    public Base() throws XException {
    public class XException extends Exception {
    public XException() {
    // etc...
    public YException extends XException {
    YException() {
    super();
    public MyClass extends Base {
    // Can this work in a constructor since YException is
    a subclass of XException?
    // Anyway to make this possible?
    public MyClass() throws YException {
    super();
    Apart from what you are asking in normal circumstances a no args constructor throwing an exception is never a good idea. The reason being that from then on anybody who extends this class would have to face problems. Consider the following
    public class MyTest
          public MyTest() throws Exception
    class Test extends MyTest
    {}this would not compile and throw the following error
    Default constructor cannot handle exception type Exception thrown by implicit super constructor. Must define an explicit constructor     
    So thats not a good practice
    cheers

  • Why constructor of rmi server throws remote exception

    why is it that the constructor of the class extending UnicastRemoteObject i.e rmi server has to throw RemoteException?

    Writing an RMI server is a matter of defining a class, exporting it when constructed and implementing one or more remote interfaces. How to export?
    auto export happens when u call super() in the constructor. It is then registered with the rmi system and made to listen to a TCP port. The various constructors for UnicatRemoteObject allow the derived classes to export at the default port.
    Because this automatic export step occurs at construction the constructors throw this exception.

  • ReferenceError: Window does not have a constructor : 327?

    I am trying to use the image processor in PS or Bridge and I get this error message:
    ReferenceError: Window does not have a constructor : 327
    I am running Mac 10.7.5 and CS5.
    -Shane

    Did you find an answer for this? I am having the same issue- even though it is two years later.
    I just installed on a new iMac from Mac Pro (so I know this version works well) and I am having the same issue.
    I pressed three buttons to erase all my saved settings..did all that to no avail...
    anyone??  thank you in advance.  I didn't want to have to re-install as I had issues the first time since my software is so old.

  • HT5577 Okay so I have 2 apple accounts. One is newer & one is my old one. The older one still has money from Itunes on it & when I go to log it will not except my password. Then it asked me my date of birth & it says its incorrect. What do I do?

    Okay so I have 2 apple accounts. One is newer & one is my old one. The older one still has money from Itunes on it & when I go to log it will not except my password. Then it asked me my date of birth & it says its incorrect.So I tried to go the email route but i never get anything in my email about recovering my password. What do I do?

    Hey donnieandjamie31,
    It sounds like you are not getting the email to reset your password.  If you have a rescue email setup for your Apple ID, the reset password email will be sent there and not your Apple ID email address:
    When you provide a rescue email address, all future security emails, such as password resets from iForgot, will be sent to the address provided.
    Rescue email address and how to reset Apple ID security questions
    http://support.apple.com/kb/HT5312
    You might want to check your other email accounts for the password reset email, including their Spam folders. If you continue to have issues, please contact our Account Security Team:
    Apple ID: Contacting Apple for help with Apple ID account security
    http://support.apple.com/kb/HT5699
    Thank you for posting in the Apple Support Communities.
    Best,
    Sheila M.

  • Okay so I have a new ipod touch...but I need to get music out of my old nano to my itues w/out wiping out my old nano...how do i do this? new laptop so itues is new...please help

    okay so I have a new ipod touch...but I need to get music out of my old nano to my itues w/out wiping out my old nano...how do i do this? new laptop so itues is new...please help

    You need to authorize your new computer in iTunes with the same account that is synced to your nano (and old iTunes)
    In the menu bar click Store >> Authorize This Computer…
    This will allow your purchased music to be re-downloaded from iTunes and played on your new computer.
    If you did not purchase the music through iTunes, you may need a third-party program to get the music off the nano. However, It is much easier (by far) to move your old library from your old computer to the new one if you still have access to the old files. iTunes will not transfer music from your nano to the computer. It is a one-way sync.
    If you still have access to your old files, search google for how to transfer an itunes library, and follow someones instructions. It's not too complicated. You may want to specify mac to pc, pc to mac, mac to mac, or pc to pc, as the instructions will change.

  • Best practice to have or to start a new financial year

    Can any one please suggest a best practice to have or to start a new financial year (FY2009-2010) in SAP B1 2007 B for a trading company started on FY2008-2009 for India?

    hi,
    Check this links
    New Fiscal Year
    how to shift to new financial year
    Requirement for next fiscal year
    Jeyakanthan

  • Okay so i have been trying to contact apple for days now but i was just thinking why do we have to only choose between white or black for the digitizer? would i be able to send my ipod into apple to get it customized to a different color besides that?

    okay so i have been trying to contact apple for days now but i was just thinking why do we have to only choose between white or black for the digitizer? would i be able to send my ipod into apple to get it customized to a different color besides that?

    Apple does not offer custom colors. I'd suggest you get a skin for your iPod. Those are available in hundreds of colors and patterns and since they're not permanent, they don't affect the iPod's warranty and you can change if you ever get tired of the skin you chose.
    Regards.

  • Why Math class does not have any constructor.

    Math class does not have any constructor.
    But all the classes have a default constructor. Then why the math class is not having any constructor. Is math class having some other constructor or is the default constructor concept applicable only for user defined classes.

    Math class does not have any constructor.
    But all the classes have a default constructor. Then
    why the math class is not having any constructor. Is
    math class having some other constructor or is the
    default constructor concept applicable only for user
    defined classes.You cannot instantiate a new Math object, nor can you extend the Math class. All you can do is use Math's static methods and variables. Its a utility class, thats what its susposed to do.
    JJ

  • Socket does not have a constructor in PPro CC

    Hi there,
    I"m trying to run a very simple script using Socket in PPro CC
            var conn = new Socket;
            if(conn.open("192.168.1.23")) {
                conn.write("GET /index.html HTTP/1.0\n\n");
                reply = conn.read(999999);
                conn.close();
    and I got this error: Socket does not have a constructor...
    Does Socket class exist in PPro ?
    If not, what would be the the way to call some Webservices?
    Thank you for the answer.
    fbm

    No older versions installed.
    I was however able to do the following:
    From Bridge I ran Photoshop>Batch. In that menu I selected an action I created in a previous version of PS that included the Fit Image script. I clicked ok and everything ran perfectly. The problem is I can't run Fit Image directly from Photoshop using conventional methods.
    As for my system info, I am running the following:
    Windows 7
    Quad core i7-3610QM @ 2.3GHz
    8GB of RAM
    nVIDIA GeForce 650M - 2GB
    750GB HD
    Could there be any other troubleshooting solution besides running reinstall? I want to exhaust all efforts before going down that path.
    Thanks.

  • Windows does not have a constructor: 244 error message

    I'm using CS6 for the first time to process some RAW files using the Photoshop Automate>Fit Image script. When I tried opening one RAW image into Photoshop to create an action I got the following error message:
    "ReferenceError: Windows does not have a constructor: 224"
    Never seen that in all my years of using Photoshop. What's causing this and how do I fix it?

    No older versions installed.
    I was however able to do the following:
    From Bridge I ran Photoshop>Batch. In that menu I selected an action I created in a previous version of PS that included the Fit Image script. I clicked ok and everything ran perfectly. The problem is I can't run Fit Image directly from Photoshop using conventional methods.
    As for my system info, I am running the following:
    Windows 7
    Quad core i7-3610QM @ 2.3GHz
    8GB of RAM
    nVIDIA GeForce 650M - 2GB
    750GB HD
    Could there be any other troubleshooting solution besides running reinstall? I want to exhaust all efforts before going down that path.
    Thanks.

  • Aerender error: "Unable to execute script at line 95. Window does not have a constructor"

    A BG Renderer user is getting this error when running aerender version 10.5.1x2:
    "Unable to execute script at line 95. Window does not have a constructor"
    The render proceeds but the process never completes which is preventing the rest of the BG Renderer notifications from triggering.  Any idea what this error means and how to fix it?
    Thanks,
    Lloyd

    I guess we'll have to wait for Adobe to answer what could be causing the transmission to drop on Line 95. Todd's out of the office for a month or two, but maybe someone else can shed some light.

  • PSCS5 Error 22: Window does not have a constructor.

    I am having a problem running scripts in PS CS5.
    When I try to run the load files into stack script, which works fine on CS5 on my laptop running Windows 7 64bit.n (i7 Processor, 4gb Ram)
    I am running the same operating system on my main pc (i7 Processor 6gb ram), and am using the 32bit version of PSCS5.
    For some reason however I get the following error when trying to run the load files into stack script.
    Error 22: Window does not have a constructor.
    Line.388
    ->         var w = new Window(res);
    I have tried trashing my preferences and reinstalling photoshop but neither has worked.
    Any suggestions on how to fix would be welcome.
    Regards
    Robyn

    Something similar has come up in the Mac-Forum:
    http://forums.adobe.com/message/2982064#2982064
    Might not be of use to you, though, but it seems to have been solved somehow …

  • CS5 - Error 22:Window does not have a constructor

    I've been experimenting with some old sccripts on the Photoshop CS5 pre-release and they seem to work fine except any that have a user interface.Scripts that run fine on CS2, CS3 and CS4 throw a "Error 22:Window does not have a constructor" error message. I was wondering if anyone else had noticed this and if they were aware of any changes as regards to scripting with CS5 and also how to correct the issue.
    CJ

    Dear All,
    I have been bothered by this same  Photoshop error 22 when trying to use the Photomerge feature or any  other of the Automate features in CS5.
    My computer is a  Windows 7 32bit machine but this should definitely apply to Windows  64bit machines and possibly the same logic may apply to Mac but I do not  know.
    I didnt find a permanent solution but I did find  a temporary fix which will at least allow you to use the function for  one opening of the program however if you close and reopen photoshop the  problem returns.
    Temporary Fix Instructions:
    1.  Locate the file "Adobe Photoshop CS5 Prefs.psp" on your computer and delete it or move it elsewhere.  (I assume by  doing this you may lose *** of your photoshop preferences but it will  allow you to use photomerge and HDR so you choose)
    2. On Windows this file can be found in the User folder under C:\**USER NAME**\AppData\Roaming\Adobe\Adobe Photoshop CS5\Adobe Photoshop CS5 Settings\ (Replace **USER NAME** with the Windows 7 account user name)
    3.  After deleting this file or temporarily moving it out of that folder  you can then open Photoshop CS5 and use the Photomerge feature as much  as you'd like.
    4. As soon as you close Photoshop CS5 the program automatically makes a new Adobe Photoshop CS5 Prefs.psp file so when you try to reopen the program it will not work again.
    I  hope this may be helpful to some people and I hope that Adobe can fix  this issue, I have not and dont plan on submitting this as a bug report  to Adobe, being that I do not personally own the program (still in 30  day trial) but anyone who would like to, please feel free to do so.

Maybe you are looking for

  • Should I buy the MBP retina now or wait for the new one?

    Hey I wanna buy a new MBP retina 15" before my uni starts in fall so after like a month and a half. I already have a 2011 MBP but wanna get a new and better one so I am just wondering what is the latest 15" retina by Apple? and when is the approximat

  • Purchase order for stock material in production order

    Hi does anybody know how to link a purchase request to production order in case of stock material ? When MRP runs aggregate all requirements for stock materials in one purchase request, and I wold like to find list of order that generated the request

  • Attaching photos to iPad email

    How do I attach a photo to an email sent from iPad? I am using gmail. Don't see an option to attach a photo.

  • Disjointed rollover...or something similar....

    Can someone tell me the best way to produce a flowchart of staff members so that when you move the mouse over the staff member's name, their photo appears. Ideally, I'd like the photo to appear in the same place on the page each time. The flowchart I

  • Java in Xcode 3.2

    Hello Everyone! I have a one quick question for you all! As xcode 3.2 is out all ready, there have been many changes to it like the operating system itself! However, I was wondering that if it is possible for me to create a Java project file in xcode