← yazılara dön

❯ cat custom-exceptions-in-c-net.md

Custom Exceptions in C#.NET

1 dk okumatr#C##Exceptions

Genellikle, büyük ölçekli projeler için (birbirine çok modüller de olabilir tabi), bir özel istisnalar tanımlamalıdır. Onun için küçük bir kod parçacıkları düşündüm umarım işinize yarar.

[Serializable]
public class CustomException : Exception
{
    public CustomException()
        : base() { }
    public CustomException(string message)
        : base(message) { }
    public CustomException(string format, params object[] args)
        : base(string.Format(format, args)) { }
    public CustomException(string message, Exception innerException)
        : base(message, innerException) { }
    public CustomException(string format, 
Exception innerException, params object[] args)
        : base(string.Format(format, args), innerException) { }
    protected CustomException(SerializationInfo info, 
StreamingContext context)
        : base(info, context) { }
}
 
Using:
 
throw new CustomException();
 
throw new CustomException(message);
 
throw new CustomException("Exception with parameter value 
                          '{0}'", param);

throw new CustomException(message, innerException); 
 
throw new CustomException("Exception with parameter value 
                          '{0}'", innerException, param)