catching a specific exception

C

cj

When I run myMAPIMessage.Send(True) if the user cancels the resulting
email it causes an exception. How do I trap this particular exception
and say that's ok?

try
myMAPIMessage.Send(True)
catch ex as ??????
'this is ok
catch ex as exception
'show these
messagebox.show(ex.message)
end try

Here is what the exception I'm getting is as shown in the command window:

? ex
{System.Runtime.InteropServices.COMException}
[System.Runtime.InteropServices.COMException]:
{System.Runtime.InteropServices.COMException}
HelpLink: "MAPI98.CHM#32001"
InnerException: Nothing
Message: "User cancelled process"
Source: "MAPIMessages"
StackTrace: " at MSMAPI.MAPIMessagesClass.Send(Object vDialog)
at WindowsApplication20.Form1.SndMAPIMail(String subj, String msg)
in C:\Documents and Settings\cj\My Documents\Visual Studio
Projects\WindowsApplication20\Form1.vb:line 565"
TargetSite: {System.Reflection.RuntimeMethodInfo}
 
G

Guest

cj,

Are all the exceptions COMExceptions?

If so, you might need to look at the exception's message to determine
whether to display an exception message. Something like:

Catch ex As Exception
If NOT ex.Message = "User cancelled process" Then
Msgbox ex.Message ...
End IF

In general I don't recommend looking at the exception's Message text to make
a decision, since the text might change in a future release, but you might
need to in this case.

Kerry Moorman
 
C

cj

I guess they would be. Now that I think of it in that light I guess if
I was using something specific to .net rather than mapi I might be able
to do it like I was thinking. Anyway, yea I can do it by looking at the
message. It'll be ok in this situation.

Kerry said:
cj,

Are all the exceptions COMExceptions?

If so, you might need to look at the exception's message to determine
whether to display an exception message. Something like:

Catch ex As Exception
If NOT ex.Message = "User cancelled process" Then
Msgbox ex.Message ...
End IF

In general I don't recommend looking at the exception's Message text to make
a decision, since the text might change in a future release, but you might
need to in this case.

Kerry Moorman

cj said:
When I run myMAPIMessage.Send(True) if the user cancels the resulting
email it causes an exception. How do I trap this particular exception
and say that's ok?

try
myMAPIMessage.Send(True)
catch ex as ??????
'this is ok
catch ex as exception
'show these
messagebox.show(ex.message)
end try

Here is what the exception I'm getting is as shown in the command window:

? ex
{System.Runtime.InteropServices.COMException}
[System.Runtime.InteropServices.COMException]:
{System.Runtime.InteropServices.COMException}
HelpLink: "MAPI98.CHM#32001"
InnerException: Nothing
Message: "User cancelled process"
Source: "MAPIMessages"
StackTrace: " at MSMAPI.MAPIMessagesClass.Send(Object vDialog)
at WindowsApplication20.Form1.SndMAPIMail(String subj, String msg)
in C:\Documents and Settings\cj\My Documents\Visual Studio
Projects\WindowsApplication20\Form1.vb:line 565"
TargetSite: {System.Reflection.RuntimeMethodInfo}
 
T

Tom Shelton

Kerry said:
cj,

Are all the exceptions COMExceptions?

If so, you might need to look at the exception's message to determine
whether to display an exception message. Something like:

Catch ex As Exception
If NOT ex.Message = "User cancelled process" Then
Msgbox ex.Message ...
End IF

In general I don't recommend looking at the exception's Message text to make
a decision, since the text might change in a future release, but you might
need to in this case.

Kerry Moorman


With a COMException, you can always look at the ErrorCode property. It
will return the HRESULT of the error. You have to look up the specific
HRESULTS, but it is a little better then using the Message property,
IMHO.
 
C

cj

I'll look into it. Thanks!

Tom said:
With a COMException, you can always look at the ErrorCode property. It
will return the HRESULT of the error. You have to look up the specific
HRESULTS, but it is a little better then using the Message property,
IMHO.
 
J

Jay B. Harlow [MVP - Outlook]

cj,
In addition to Tom's suggestion, you can use the When clause of the catch to
catch an exception based on specific attributes.

For example:

To only catch COM E_NOT_FOUND exceptions (in CDO) I use:

Try
Return GetField(name)
Catch ex As COMException When ex.ErrorCode =
MAPI.CdoErrorType.CdoE_NOT_FOUND
Return AddField(name, type)
End Try


Or to catch WebExceptions from HttpWebResponses, I would use:

Try
m_response = DirectCast(m_request.GetResponse(),
HttpWebResponse)
Catch ex As WebException When TypeOf ex.Response Is HttpWebResponse
m_response = DirectCast(ex.Response, HttpWebResponse)
End Try

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| When I run myMAPIMessage.Send(True) if the user cancels the resulting
| email it causes an exception. How do I trap this particular exception
| and say that's ok?
|
| try
| myMAPIMessage.Send(True)
| catch ex as ??????
| 'this is ok
| catch ex as exception
| 'show these
| messagebox.show(ex.message)
| end try
|
| Here is what the exception I'm getting is as shown in the command window:
|
| ? ex
| {System.Runtime.InteropServices.COMException}
| [System.Runtime.InteropServices.COMException]:
| {System.Runtime.InteropServices.COMException}
| HelpLink: "MAPI98.CHM#32001"
| InnerException: Nothing
| Message: "User cancelled process"
| Source: "MAPIMessages"
| StackTrace: " at MSMAPI.MAPIMessagesClass.Send(Object vDialog)
| at WindowsApplication20.Form1.SndMAPIMail(String subj, String msg)
| in C:\Documents and Settings\cj\My Documents\Visual Studio
| Projects\WindowsApplication20\Form1.vb:line 565"
| TargetSite: {System.Reflection.RuntimeMethodInfo}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top