Jump to content

Exception before or after

- - - - -

  • Please log in to reply
1 reply to this topic

#1
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
Hei,

I've seen a few styles on exceptions and particularly when to raise them in a class.
A concrete example should clarify:

A calls B's constructor (ctor) with argument file path.
The file path could be false.
Should B check the file path in the ctor and throw or merely save the value to attribute and throw when it's used if need be?


There are no concrete absolute truths on something like this but I'd like different points of view on this.
Not throwing in the ctor makes it easier to use since exception isn't possible and when applying
this idea more broadly, there could be various other methods that could be handled without a risk of
exception if the ctor checks the path before hand.

Thoughts?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
I'd say that either is a valid choice, as long as you document your choice well.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users