Delen via


CA1064: Uitzonderingen moeten openbaar zijn

Eigenschappen Weergegeven als
Regel-id CA1064
Titel Uitzonderingen moeten openbaar zijn
Categorie Ontwerpen
Oplossing is brekend of niet-brekend Niet-brekend
Standaard ingeschakeld in .NET 8 Nee

Oorzaak

Een niet-openbare uitzondering is rechtstreeks afgeleid van Exception, SystemExceptionof ApplicationException.

Beschrijving van regel

Een interne uitzondering is alleen zichtbaar binnen een eigen intern bereik. Nadat de uitzondering buiten het interne bereik valt, kan alleen de basisonderzondering worden gebruikt om de uitzondering te ondervangen. Als de interne uitzondering wordt overgenomen van Exception, SystemExceptionof ApplicationException, beschikt de externe code niet over voldoende informatie om te weten wat er met de uitzondering moet worden uitgevoerd.

Maar als de code een openbare uitzondering heeft die later wordt gebruikt als basis voor een interne uitzondering, is het redelijk om ervan uit te gaan dat de code verder uit kan gaan, iets intelligents kan doen met de basisuitzondering. De openbare uitzondering bevat meer informatie dan wordt verstrekt door Exception, SystemExceptionof ApplicationException.

Schendingen oplossen

Maak de uitzondering openbaar of leid de interne uitzondering af van een openbare uitzondering die niet Exception, SystemExceptionof ApplicationException.

Wanneer waarschuwingen onderdrukken

Onderdrukt een bericht van deze regel als u zeker weet dat de privéuitzondering binnen een eigen intern bereik valt.

Een waarschuwing onderdrukken

Als u slechts één schending wilt onderdrukken, voegt u preprocessorrichtlijnen toe aan uw bronbestand om de regel uit te schakelen en vervolgens opnieuw in te schakelen.

#pragma warning disable CA1064
// The code that's violating the rule is on this line.
#pragma warning restore CA1064

Als u de regel voor een bestand, map of project wilt uitschakelen, stelt u de ernst none ervan in op het configuratiebestand.

[*.{cs,vb}]
dotnet_diagnostic.CA1064.severity = none

Zie Codeanalysewaarschuwingen onderdrukken voor meer informatie.

Voorbeeld

Deze regel wordt geactiveerd op de eerste voorbeeldmethode FirstCustomException, omdat de uitzonderingsklasse rechtstreeks is afgeleid van Uitzondering en intern is. De regel wordt niet geactiveerd op de klasse SecondCustomException, omdat hoewel de klasse ook rechtstreeks is afgeleid van Uitzondering, de klasse openbaar wordt verklaard. De derde klasse ontslaat de regel ook niet omdat deze niet rechtstreeks is afgeleid van System.Exception, System.SystemExceptionof System.ApplicationException.

// Violates this rule
[Serializable]
internal class FirstCustomException : Exception
{
    internal FirstCustomException()
    {
    }

    internal FirstCustomException(string message)
        : base(message)
    {
    }

    internal FirstCustomException(string message, Exception innerException)
        : base(message, innerException)
    {
    }

    protected FirstCustomException(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
    }
}

// Does not violate this rule because
// SecondCustomException is public
[Serializable]
public class SecondCustomException : Exception
{
    public SecondCustomException()
    {
    }

    public SecondCustomException(string message)
        : base(message)
    {

    }

    public SecondCustomException(string message, Exception innerException)
        : base(message, innerException)
    {
    }

    protected SecondCustomException(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
    }
}

// Does not violate this rule because
// ThirdCustomException it does not derive directly from
// Exception, SystemException, or ApplicationException
[Serializable]
internal class ThirdCustomException : SecondCustomException
{
    internal ThirdCustomException()
    {
    }

    internal ThirdCustomException(string message)
        : base(message)
    {
    }

    internal ThirdCustomException(string message, Exception innerException)
        : base(message, innerException)
    {
    }


    protected ThirdCustomException(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
    }
}