catching a specific exception

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cj

    #1

    catching a specific exception

    When I run myMAPIMessage.S end(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.S end(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 .InteropService s.COMException}
    [System.Runtime. InteropServices .COMException]:
    {System.Runtime .InteropService s.COMException}
    HelpLink: "MAPI98.CHM#320 01"
    InnerException: Nothing
    Message: "User cancelled process"
    Source: "MAPIMessag es"
    StackTrace: " at MSMAPI.MAPIMess agesClass.Send( Object vDialog)
    at WindowsApplicat ion20.Form1.Snd MAPIMail(String subj, String msg)
    in C:\Documents and Settings\cj\My Documents\Visua l Studio
    Projects\Window sApplication20\ Form1.vb:line 565"
    TargetSite: {System.Reflect ion.RuntimeMeth odInfo}
  • Kerry Moorman

    #2
    RE: catching a specific exception


    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" wrote:
    [color=blue]
    > When I run myMAPIMessage.S end(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.S end(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 .InteropService s.COMException}
    > [System.Runtime. InteropServices .COMException]:
    > {System.Runtime .InteropService s.COMException}
    > HelpLink: "MAPI98.CHM#320 01"
    > InnerException: Nothing
    > Message: "User cancelled process"
    > Source: "MAPIMessag es"
    > StackTrace: " at MSMAPI.MAPIMess agesClass.Send( Object vDialog)
    > at WindowsApplicat ion20.Form1.Snd MAPIMail(String subj, String msg)
    > in C:\Documents and Settings\cj\My Documents\Visua l Studio
    > Projects\Window sApplication20\ Form1.vb:line 565"
    > TargetSite: {System.Reflect ion.RuntimeMeth odInfo}
    >[/color]

    Comment

    • cj

      #3
      Re: catching a specific exception

      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 Moorman wrote:[color=blue]
      > 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" wrote:
      >[color=green]
      >> When I run myMAPIMessage.S end(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.S end(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 .InteropService s.COMException}
      >> [System.Runtime. InteropServices .COMException]:
      >> {System.Runtime .InteropService s.COMException}
      >> HelpLink: "MAPI98.CHM#320 01"
      >> InnerException: Nothing
      >> Message: "User cancelled process"
      >> Source: "MAPIMessag es"
      >> StackTrace: " at MSMAPI.MAPIMess agesClass.Send( Object vDialog)
      >> at WindowsApplicat ion20.Form1.Snd MAPIMail(String subj, String msg)
      >> in C:\Documents and Settings\cj\My Documents\Visua l Studio
      >> Projects\Window sApplication20\ Form1.vb:line 565"
      >> TargetSite: {System.Reflect ion.RuntimeMeth odInfo}
      >>[/color][/color]

      Comment

      • Tom Shelton

        #4
        Re: catching a specific exception


        Kerry Moorman wrote:[color=blue]
        > 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[/color]


        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.

        --
        Tom Shelton [MVP]

        Comment

        • cj

          #5
          Re: catching a specific exception

          I'll look into it. Thanks!

          Tom Shelton wrote:[color=blue]
          > Kerry Moorman wrote:[color=green]
          >> 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[/color]
          >
          >
          > 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.
          >
          > --
          > Tom Shelton [MVP]
          >[/color]

          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: catching a specific exception

            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.CdoErrorTy pe.CdoE_NOT_FOU ND
            Return AddField(name, type)
            End Try


            Or to catch WebExceptions from HttpWebResponse s, I would use:

            Try
            m_response = DirectCast(m_re quest.GetRespon se(),
            HttpWebResponse )
            Catch ex As WebException When TypeOf ex.Response Is HttpWebResponse
            m_response = DirectCast(ex.R esponse, HttpWebResponse )
            End Try

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


            "cj" <cj@nospam.nosp am> wrote in message
            news:uiY$PX3eGH A.2188@TK2MSFTN GP04.phx.gbl...
            | When I run myMAPIMessage.S end(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.S end(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 .InteropService s.COMException}
            | [System.Runtime. InteropServices .COMException]:
            | {System.Runtime .InteropService s.COMException}
            | HelpLink: "MAPI98.CHM#320 01"
            | InnerException: Nothing
            | Message: "User cancelled process"
            | Source: "MAPIMessag es"
            | StackTrace: " at MSMAPI.MAPIMess agesClass.Send( Object vDialog)
            | at WindowsApplicat ion20.Form1.Snd MAPIMail(String subj, String msg)
            | in C:\Documents and Settings\cj\My Documents\Visua l Studio
            | Projects\Window sApplication20\ Form1.vb:line 565"
            | TargetSite: {System.Reflect ion.RuntimeMeth odInfo}


            Comment

            Working...