When to throw exceptions and when to write them down?

I have a three-layer structure
1. Presentation level
2. Business level
3. Data
level The presentation level interacts with the business level using the service facade. Now I am confused about where I should use my exceptions, where I should write them down and where I should catch and internalize them. I am currently swallowing my exception at my presentation level after logging, while I am registering and throwing them everywhere. This causes the exception to be thrice recorded. This is not a big concern, but I would like to know how to do it.

For example: Presentation level code:

try 
{
  UserService.GetAllUsers() ;
}
catch(Exception ex )
{
  Logger.log(ex) // exception gets logged here
  // redirect to a friendly user error page 
}

UserService Layer Code

try
{
  IUserDAO userDAO = ServiceRegistry.GetRegistry().GetDAOFactory().GetUserDAO() ;
  return userDao.GetAllUsers() ;
}
catch ( Exception ex)
{
  Logger.log(ex) ;  // and here !
  throw;
}

DAOLayer Code

try
{
  // All db interaction code 
}
catch(Exception ex)
{
  Logger.log(ex) //and here !!
  throw ;
}
+3
5

try-catch. , "DAOLayer" , 3 ...

- ! , , , - , , - .

, ( ), 2 , .NET . - AppDomain.CurrentDomain.UnhandledException, - Application.ThreadException( Windows.Forms).

+4

, . , , , , . ex.StackTrace, , .

, - , , , , , , . catch, (, - , ). , , .

, , , - , . .

+1

, - ... , , - .

! , , .

. . . , - ?

+1

, , , , .

, , , , , , , .

, , . , , , - , .

- , , .

+1

, StackTrace, , ex.Data, , , ex.Data.Add("filename", filename). . , , .

0

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


All Articles