locked
try catch loop not catching RRS feed

  • Question

  • i have the following in a asp.net page.  if i dont pass an ownerid it errors out the page versus the try catch.  How do i stop that?

    try

     

     

    Dim NewOwner As SecurityPrincipal = New SecurityPrincipal

    NewOwner.PrincipalId =

    New Guid(OwnerID)

    NewOwner.Type = SecurityPrincipalType.User

     

    Dim TaskTarget As TargetOwnedTask = New TargetOwnedTask

    TaskTarget.EntityId = TaskID

     

    Dim AssignOwner As AssignRequest = New AssignRequest

    AssignOwner.Assignee = NewOwner

    AssignOwner.Target = TaskTarget

     

    Dim AssignResponse As AssignResponse = CType(crmService.Execute(AssignOwner), AssignResponse)

     

     Catch exAssign As System.Web.Services.Protocols.SoapException

     

     

    End Try

    Monday, March 8, 2010 9:58 PM

Answers

  • You've explicitly defined the catch statement with the type System.Web.Services.Protocols.SoapException.  If the exception being thrown is not of that type, then your catch will not "catch" it.  Please validate the type of exception thrown by reproducing the error and examining the server's response.  You may have to enable DevErrors .  If you can't interpret the response, post it here.

    There are two paths to resolving this problem, if there is a mismatch between the type you're catching, and the type being thrown:

    1.) Replace or add a catch statement with the proper type.
    2.) Replace or add a catch statement with a more generic type.  (For example:  System.Exception should catch all exceptions)
    Dave Berry
    • Proposed as answer by Mayank Pujara Tuesday, March 9, 2010 12:51 PM
    • Marked as answer by Jim Glass Jr Tuesday, March 9, 2010 4:06 PM
    Monday, March 8, 2010 10:21 PM
    Moderator
  • it wouldnt catch on the generic system.exception either
    Not in production code, but while developing it can be useful.

    I bet this code fails on new Guid(Ownerid) if you don't provide an ownerid. Instead of catching the exception, you can better make sure that you never reach that line if no ownerid is provided.
    • Marked as answer by Jim Glass Jr Tuesday, March 9, 2010 4:06 PM
    Tuesday, March 9, 2010 2:53 PM

All replies

  • You've explicitly defined the catch statement with the type System.Web.Services.Protocols.SoapException.  If the exception being thrown is not of that type, then your catch will not "catch" it.  Please validate the type of exception thrown by reproducing the error and examining the server's response.  You may have to enable DevErrors .  If you can't interpret the response, post it here.

    There are two paths to resolving this problem, if there is a mismatch between the type you're catching, and the type being thrown:

    1.) Replace or add a catch statement with the proper type.
    2.) Replace or add a catch statement with a more generic type.  (For example:  System.Exception should catch all exceptions)
    Dave Berry
    • Proposed as answer by Mayank Pujara Tuesday, March 9, 2010 12:51 PM
    • Marked as answer by Jim Glass Jr Tuesday, March 9, 2010 4:06 PM
    Monday, March 8, 2010 10:21 PM
    Moderator
  • it wouldnt catch on the generic system.exception either
    Tuesday, March 9, 2010 2:00 PM
  • it wouldnt catch on the generic system.exception either
    Not in production code, but while developing it can be useful.

    I bet this code fails on new Guid(Ownerid) if you don't provide an ownerid. Instead of catching the exception, you can better make sure that you never reach that line if no ownerid is provided.
    • Marked as answer by Jim Glass Jr Tuesday, March 9, 2010 4:06 PM
    Tuesday, March 9, 2010 2:53 PM
  • so i am checking for the ownerid but is there a way to catch it?

    Tuesday, March 9, 2010 5:31 PM
  • Does it catch just the keyword "Exception"?  See: http://www.homeandlearn.co.uk/NET/nets5p4.html
    Dave Berry
    Tuesday, March 9, 2010 5:47 PM
    Moderator
  • yeh -- it doesnt catch even with just exception

    try

    catch ex as exception

    end try

    it doesnt  enter the catch
    Tuesday, March 9, 2010 9:50 PM
  • Grab your debugger and step through the code to see what's happening.
    Wednesday, March 10, 2010 7:08 AM