Code Review e custom exception

Ogni volta che faccio code review di codice vedo sempre sempre sempre le invenzioni più strane per creare eccezioni Custom. Basterebbe utilizzare lo snippet exception per creare un’eccezione perfetta da Best Practice: Serializable, con tutti gli overload corretti:

   1: /// <summary>
   2: /// My Exception class.
   3: /// </summary>
   4: [Serializable]
   5: public class MyException : Exception
   6: {
   7:     //
   8:     // For guidelines regarding the creation of new exception types, see
   9:     //    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp
  10:     // and
  11:     //    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp07192001.asp
  12:     //
  13:  
  14:     /// <summary>
  15:     /// Initializes a new instance of the <see cref="MyException"/> class.
  16:     /// </summary>
  17:     public MyException()
  18:     {
  19:     }
  20:  
  21:     /// <summary>
  22:     /// Initializes a new instance of the <see cref="MyException"/> class.
  23:     /// </summary>
  24:     /// <param name="message">The message.</param>
  25:     public MyException(string message) : base(message)
  26:     {
  27:     }
  28:  
  29:     /// <summary>
  30:     /// Initializes a new instance of the <see cref="MyException"/> class.
  31:     /// </summary>
  32:     /// <param name="message">The message.</param>
  33:     /// <param name="inner">The inner.</param>
  34:     public MyException(string message, Exception inner) : base(message, inner)
  35:     {
  36:     }
  37:  
  38:     /// <summary>
  39:     /// Initializes a new instance of the <see cref="MyException"/> class.
  40:     /// </summary>
  41:     /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
  42:     /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
  43:     /// <exception cref="T:System.ArgumentNullException">
  44:     /// The <paramref name="info"/> parameter is null.
  45:     /// </exception>
  46:     /// <exception cref="T:System.Runtime.Serialization.SerializationException">
  47:     /// The class name is null or <see cref="P:System.Exception.HResult"/> is zero (0).
  48:     /// </exception>
  49:     protected MyException(
  50:         SerializationInfo info,
  51:         StreamingContext context) : base(info, context)
  52:     {
  53:     }
  54: }

E con un tocco di Resharper (F12 per prossimo warning/errore) e GhostDoc (CRTL+Shift+D per documentare) la documentazione si fa quasi in automatico (ho scritto solo il commento della classe), così evitiamo qualche warning del compiler eh ?

Nell’ultima code review c’erano circa 700 warning su una solution con 15 progettini ….. Non male eh ?

posted @ giovedì 29 gennaio 2009 02:08

Print
Comments have been closed on this topic.