Catch none-zero final status with DicomAssociation

So far in DicomObjects, we don't throw exceptions when none-zero final status is received during the DICOM associations. The none-zero final status get passed by us and it should be the developers' responsibilities to check and, if needed, alert the user for any warnings or failures.

To catch the final status and also the related warning/error fields, you can take a look at the following code (vb6 for COM version and vb.net for the .NET version of DicomObjects).

Contents

COM Version

SCP - Setting the Error Code, Offending Element and Error Comment

It is only possible when using Asynchronous mode in the COM version that you can set the Offending Element Field (0000H, 0901H) and the Error Comment Field (0000H, 0902H):

Private Sub DicomServer1_ImageReceivedAsync(ByVal Connection As DicomObjects.DicomConnection, 
                                            ByVal Image As DicomObjects.DicomImage)
 Connection.Errors.Attributes.Add &H0000, &H0901, &H12345678
 Connection.Errors.Attributes.Add &H0000, &H0902, "My Error Comment"
 Connection.SendStatus &HC000
End Sub

SCU - Checking the Final Status and Populating the Offending Element and Error Comment

Keep in mind Offending Element and Error Comment are OPTIONAL, so they may or may not be present in the returned response. You need to make sure they are present before trying to access their values.

Dim v as Variant
Dim list as String
' cn here is a DicomConnection object, you check the returned final status after an operation, i.e. SendImages
If cn.LastStatus <> 0 Then
  If cn.ReturnedResponses(cn.ReturnedResponses.Count).Attributes(&H0, &H901).Exists And 
     cn.ReturnedResponses(cn.ReturnedResponses.Count).Attributes(&H0, &H901).Value <>  Nothing Then
    v = cn.ReturnedResponses(cn.ReturnedResponses.Count).Attributes(&H0, &H901).Value
    If (VarType(v) > 8192) Then ' We expect an array here
       For i = LBound(v, 1) To UBound(v, 1)
          list = list & Hex(v(i))
       Next
    End If
    MsgBox "LastStatus - " & cn.LastStatus
    MsgBox "Error Element - " & list
    MsgBox "Error Comment - " & cn.ReturnedResponses(cn.ReturnedResponses.Count).Attributes(&H0, &H902).Value
  End If
End If

.NET 5.4 and all previous versions

SCP - Setting the Values

Private Sub Server_InstanceReceived(ByVal sender As Object, ByVal e As DicomObjects.DicomServer.InstanceReceivedArgs) Handles Server.InstanceReceived
' For example if There is something wrong with the e.Instance, we set the final status to none-zero
 e.Errors.Add(&H0, &H901, &H12345678)
 e.Errors.Add(&H0, &H902, "My Error Comment")
 e.Status = &HC000
End Sub

SCU - Checking the status and populating the information

cn.SendInstances(...)
If cn.LastStatus <> 0 Then
   MsgBox "LastStatus - " & cn.LastStatus
   MsgBox "Error Element - " & cn.LastCommand(0, &H901).ToString()
   MsgBox "Error Comment - " & cn.LastCommand(0, &H902).ToString()
End If
cn.Close()

Note, in the 5.4 and all previous versions, you have to use DicomAssociation object for sending instances, if you use DicomImage.Send method, you won't be able to pick up the final status, the Offending Element and Error Comment. You won't get any exception thrown by DicomObjects if the final status is not zero (success).

.NET 5.5 (16th of May 2008) and all later versions

SCP - No Difference to the 5.4 version

SCU - You have DefaultThrowStatusException

In the 5.5 (16th of May 2008) and all later versions you will have a static DicomAssociation property called DefaultThrowStatusException. The default value is false so that no one's existing program will be affected by this new addition. If set to true, DicomObjects will throw an exception when a none-zero final status is received, and developers have to put a try catch block to make sure the exception is handled in a proper way.

DicomAssociation.DefaultThrowStatusException = True
Try
   ' You can use both the DicomImage.Send method and the DicomAssociation.SendInstances method
   DicomImage1.Send(...)
Catch ex As DicomException
   MsgBox(ex.Message)
End Try

You can also set the DefaultThrowStatusException behaviour on individual DicomAssociation basis, during the DICOM association.

DicomAssociation.DefaultThrowStatusException = True ' global setting to enable exceptions to be thrown
Dim cn As New DicomAssociation()
cn.Open(...)
cn.ThrowStatusException = False ' Disable exceptions on this association, even when it is already established 
cn.SendInstances(...)

If cn.LastStatus <> 0 Then
   MsgBox "LastStatus - " & cn.LastStatus
   MsgBox "Error Element - " & cn.LastCommand(0, &H901).ToString()
   MsgBox "Error Comment - " & cn.LastCommand(0, &H902).ToString()
End If
cn.Close()
Relevance: