## Exceptions

The CORBA standard defines two different kinds of exceptions: User Exceptions and System Exceptions.

### User Exceptions

#### Throwing (sending) an user exception

If writing a server in VB user exceptions will be thrown by the following code. The execute method of the skeleton code test if a user exception is set and send them over the wire to the client.
`
Dim oMyEx As c_MyFileNotFoundException
Set oMyEx = New c_MyFileNotFoundException
oMyEx.fileName = "xy"
Call VBOrb.raiseUserEx(oMyEx)`

#### Catching (receiving) an user exception

The invokeReq method of class cOrbObjRef receives the user exception and put them in a normal exception. Here is the code which can be used by the client to handle user exceptions.
`
Public Function myDoFoo ...
On Error GoTo ErrHandler
Call remoteObject.method()
...
Exit Function
ExHandler:
Dim oEx As VBOrb.cOrbException
Set oEx = VBOrb.getException()
'If oEx is c_MyFileNotFoundException Then ...
Select Case TypeName(oEx)
Case "c_MyFileNotFoundException"
Dim oMyEx as c_MyFileNotFoundException
Set oMyEx = oEx
'Do something with oMyEx...
Case Else
' Unexpected user exception
End Select
Exit Function
ErrHandler:
If VBOrb.ErrIsUserEx() Then Resume ExHandler
Call VBOrb.ErrReraise("myDoFoo")
End Function`

### System Exceptions

System exceptions are standard exception types, which are defined by the CORBA specification and are used by the Object Request Broker (ORB) and object adapters (OA). Standard exceptions may be returned as a result of any operation invocation, regardless of the interface on which the requested operation was attempted. There are two aspects to the mapping of System Exceptions. One aspect is generating an appropriate Err.Number for the operation to return. The other aspect is conveying System Exception information via the standard Error Object.

The Err.Description of a System Exception looks like:

CORBA System Exception: [<exception repository id>] minor code [<minor code>][<completion status>]

where the <exception repository id> and <minor code> are those of the CORBA system exception. <completion status> is YES, NO, or MAYBE based upon the value of the system exception's CORBA completion status.

#### Throwing (sending) system exceptions

An example of throwing a system exceptions is shown by the following code.
`
Call VBOrb.raiseBADPARAM(1, VBOrb.CompletedNO)`

#### Catching (receiving) system exceptions

The invokeReq method of class cOrbObjRef receives the system exception and put them into the error object. Here is the code which can be used by the client to handle system exceptions.
`
Public Function myDoFoo ...
On Error GoTo ErrHandler
Call remoteObject.method()
...
Exit Function
ObjNotExist:
...
Exit Function
ErrHandler:
If VBOrb.ErrIsSystemEx() Then
'Do something with Err.Description
If Err.Number = (VBOrb.ITF_E_OBJECT_NOT_EXIST_NO Or vbObjectError) Then
Resume ObjNotExist
End If
End If
Call VBOrb.ErrReraise("myDoFoo")
End Function`