• Frans Bouma (unregistered)

    But... constructors do fail sometimes. So it's not that weird to test if the constructor succeeded, especially if the constructor does things which can fail (open a connection or something).

    (But perhaps my lack of VB.NET skillz stops me from seeing the weirdness).

    I'm more concerned about throwing an exception without a description

  • Alex Papadimoulis (unregistered)

    Frans makes an good point. But, when a constructor fails, it will throw exception, not refuse to make an instance. Proper code:

    try { myInstance = new myClass(); }
    catch { ... }

  • Jerry Pisk (unregistered)

    I think there's an "On Error Resume Next" statement at the top if the file... In that case the logic would make sense in the context it's used in.

  • John Bush (unregistered)

    The error handling is inconsistent, too.

    If you pass the function an intNewsId of less than one, it returns the string 'Invalid ID'.

    But if New() fails, it throws an exception with no description.

    WTF?

  • Bobby V. (unregistered)

    I'm a C# programmer, not a VB.NET programmer, but if there is an "On Error Resume Next" statement at the top, wouldn't the Exception they thrown be suppressed by the "On Error Resume Next"? Personally, any coder that uses "On Error..." should be shot. I've debugged way too much poorly written VB6 and ASP code what hard to identify problems due to the "On Error...".

    Alex is correct in the way to code this.

  • Vijay (unregistered)

    Ok. I might be totally off the mark on his but just a thought.

    hmm now wait a minute... If i remember right, at the point where the constructor logic is executed, the object must have already been allocated and the initialisation logic only needs to be done in the constructor. Now if some one is stupid enough to probably throw an exception in the constructor, then that will definitely result in a null object allocation ( not sure here too .. ) This exception might be thrown just because some resource could not be initialized or whatsoever but then the object memory allocation would not be null even then !

    But like you said, if there is a "On Error Resume Next" statement at the top, then all i can see is that exception will happen at the first line, but it will continue, and from then on, everytime the code tries to access objNews, it should keep throwing an exception but code will keep ignoring it ..

    I'd definitely say WTF aloud for this one ;-)
    Any comments ?

  • Frans Bouma (unregistered)

    "But, when a constructor fails, it will throw exception, not refuse to make an instance. Proper code: "
    You're right, I forgot about that :) It's so obvious that I totally overlooked that completely :D

  • Jake Vinson (unregistered)

    Wow, John Bush, the singer of Anthrax reads your blog. That kicks ass!

  • Delphi7 (unregistered)

    What happens if the exception constructor fails
    (New Exception)

  • Bill Moore (unregistered) in reply to Bobby V.

    You nailed that one

Leave a comment on “Because New() Fails Way Too Much”

Log In or post as a guest

Replying to comment #:

« Return to Article