Jump to content

To raise exception or not ?

- - - - -

  • Please log in to reply
5 replies to this topic

#1
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
I have the following:
bool operator==(Siirto& rSiirto)
{
    if(from == rSiirto.from && to == rSiirto.to)
       return true;
    else
       return false;
}
I'm in the process of improving my code and thus making it more easy to test.
Unit testing is a big part of it.
Let's say the variable rSiirto == null.
Should this be an exception or just return false?
I'm thinking this is matter of taste and preference but I read that one should only
raise exceptions if the condition is exceptional so you should never raise exceptions
unless something truly exceptional has happened..

Thoughts?

#2
JCoder

JCoder

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 245 posts
I think you should simply return false in this case.

BTW:
1. Good code does not need null.
2. Exception-safe C++ coding is difficult.

#3
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
It depends. Is rSiirto being null an exception condition?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
I don't think it possibly could be. It's just an operator==

#5
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
if rSiirto is null, you'll generate an access violation.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
julmuri

julmuri

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts
Hmm rSiirto is reference so it can not be null. Afaik.
So your function should be just fine.
std::string s("oberq zhpu?");std::for_each(s.begin(),s.end(),[&](char&c){c=~c;c=~c-0x01/(~(c|0x20)/0x0D*0x02-0x0B)*0x0D;});std::cout<<s;




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users