Why error function prints Exception many times