C ++ Exception Handling Issues

1) For convenience, I have my entire program in a try block. That way, I can make an exception at any time in my code and know that it will be processed the same way. As the program gets larger, will this method cause a performance hit?

2) If the objects are not selected when leaving the scope, then why throw a temporary object into action? eg:.

class Error: public std :: exception  
{
  private:
    char * m;
  private:
    Error (char * l): m (l) {}
    virtual char * what ()
    {
      return m;
    }
};

int main ()
{
  try
  {
    throw Error ("test");
  }
  catch (std :: exception & e)
  {
    puts (e.what ());
    return -1;
  }
  return 0;
}

throw, , try?

3) Windows, , what() STL char*? wchar_t*?

+3
2

, . . .

, catch, . :

catch (std::exception & e)
{
    throw e;  // bad, always throws std::exception rather than what was caught
}

catch (std::exception & e)
{
    throw;  // good, rethrows the exact copy that you caught without making another copy
}

P.S. , , UTF-8 what. UTF-16 Windows I/O. Unicode, , Windows.

+9

1) try , ,

2) , . , , , . , , .

3) std:: exception, , char*. wchar, .

, main(), , .

+4

Source: https://habr.com/ru/post/1770199/


All Articles