Is it bad practice for an object to catch its own exception and keep the message in the property?

Say I have a list of objects of the same type. I want to iterate over this list of objects and execute a “dangerous” method for each of them. If an exception occurs in this method, is it a bad practice for the method to catch the exception and set the error property for the object?

Here is a brief example where the Start () Car method catches its own exception and stores it in the Problem property:

Sub Main()

    // Build cars.
    Dim ford As New Car("ford", "1988")
    Dim chevy As New Car("chevy", "1992")
    Dim honda As New Car("honda", "2002")

    // Create a list of cars.
    Dim carList As New List(Of Car)
    carList.Add(ford)
    carList.Add(chevy)
    carList.Add(honda)

    // Start all of the cars.
    For Each c As Car In carList
        // I don't want to stop processing if an exception occurs.
        // And I don't care to report any errors now.
        c.Start()
    Next

    // other stuff...

    // Now report errors.
    Dim cr As New CarReport
    cr.ReportProblems(carList)

End Sub

A class that reports any problems:

Public Class CarReport

    Public Sub ReportProblems(ByVal c As List(Of Car))
        // Report any problems.
        For Each c In carList
            If Not String.IsNullOrEmpty(c.Problem) Then
                Console.WriteLine(c.Problem)
            End If
        Next
    End Sub

End Class

Simple car class:

Public Class Car

    Private _make As String
    Private _year As String
    Private _problem As String = String.Empty

    Public Sub New(ByVal make As String, ByVal year As String)
        _make = make
        _year = year
    End Sub

    Public Sub Start()
        Try
            // Do something that could throw an exception.
        Catch ex As Exception
            _problem = "Car would not start."
        End Try
    End Sub

    Public ReadOnly Property Problem() As String
        Get
            Return _problem
        End Get
    End Property

End Class

: , (.. Sub Main() ) Problem Car? . , , - Car, .

: - , , .

+3
7

, . , , ? , , .

try/catch, . , , , , / , , .

+4

, , ADO.Net.

HasErrors. .

: " WPF"

EDIT: @casperOne: . , HasError - , - / , / reset .

. "" , .

, " !";)

+4

:

(1) Fail Fast, .

(2) . carReport .

(3) , main(), , , , - .

(4) , , , , , .

(5) . car.problem car.start . , , car.start , ? ? , car.problem . .

+3

, " " " ".
:

, , ADO.NET HasErrors, IDataErrorInfo, , , . , validtion - , , .

, , ( NullReferenceException MemoryException), , undefined, , JohnFx.

+1
var errors = new string[carList.Length];
for(var i = 0; i < carList.Length; i += 1) {
    try {
        carList[i].Start();
    } catch(Exception ex) {
        errors[i] = ex.Message;
    }
}
+1

, , . , , , , , , , . , , , .

patern ADO.Net, "Tom A" , , . statet, . / , ! , , , , .

0

, , . , , , . , . , Start , - Func<Exception,someEnumeratedType> (, Action<Exception,somethingElse>). , Exception Action; , , , .

I would suggest that objects should support the "crash when" flag when and only if the operation would be caused by the fact that the object is in a state that is less than useful. Operations on an object whose state is damaged should throw exceptions in addition to setting a flag, unless the calling object is known to be ready to work with an error.

0
source

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


All Articles