Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ArgumentException

ArgumentException

Scheduled Pinned Locked Moved C#
mysqlhelpquestioncareer
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    si_69
    wrote on last edited by
    #1

    Hi I have a web method (see below) which accepts an integer as a parameter. I want to be able to catch the error if a paramter is not provided. I thought using catch (ArgumentException) would do the job Is there anything else i should be doing to be able to catch errors like this ? thanks Simon

    \[WebMethod\]
    public DataSet GetOrganisation(int organisationId)
    {
    
        DataSet ds = new DataSet();
        try
        {
    

    .....

        }
        catch (MySql.Data.MySqlClient.MySqlException ex)
        {
            WriteToLog(organisationId.ToString(), "", "GetOrganisation", ex.Message);
        }
        catch (ArgumentException argerr)
        {
            WriteToLog(organisationId.ToString(), "", "GetOrganisation", argerr.Message);
        }
    
       
        return ds;
    }
    
    S N E J 4 Replies Last reply
    0
    • S si_69

      Hi I have a web method (see below) which accepts an integer as a parameter. I want to be able to catch the error if a paramter is not provided. I thought using catch (ArgumentException) would do the job Is there anything else i should be doing to be able to catch errors like this ? thanks Simon

      \[WebMethod\]
      public DataSet GetOrganisation(int organisationId)
      {
      
          DataSet ds = new DataSet();
          try
          {
      

      .....

          }
          catch (MySql.Data.MySqlClient.MySqlException ex)
          {
              WriteToLog(organisationId.ToString(), "", "GetOrganisation", ex.Message);
          }
          catch (ArgumentException argerr)
          {
              WriteToLog(organisationId.ToString(), "", "GetOrganisation", argerr.Message);
          }
      
         
          return ds;
      }
      
      S Offline
      S Offline
      SledgeHammer01
      wrote on last edited by
      #2

      huh? you can't pass anything BUT an int to that method. Its not an optional parameter.

      A 1 Reply Last reply
      0
      • S si_69

        Hi I have a web method (see below) which accepts an integer as a parameter. I want to be able to catch the error if a paramter is not provided. I thought using catch (ArgumentException) would do the job Is there anything else i should be doing to be able to catch errors like this ? thanks Simon

        \[WebMethod\]
        public DataSet GetOrganisation(int organisationId)
        {
        
            DataSet ds = new DataSet();
            try
            {
        

        .....

            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                WriteToLog(organisationId.ToString(), "", "GetOrganisation", ex.Message);
            }
            catch (ArgumentException argerr)
            {
                WriteToLog(organisationId.ToString(), "", "GetOrganisation", argerr.Message);
            }
        
           
            return ds;
        }
        
        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        As long as you pass an integer, any integer, to this method no exception will be generated. What you can do is check for a valid range then throw an exception if the variable passed to this method doesn't fall within that range. This can be done in several ways, hard coded, using Validation Application Block or Code Contracts. It's up to you.


        No comment

        1 Reply Last reply
        0
        • S SledgeHammer01

          huh? you can't pass anything BUT an int to that method. Its not an optional parameter.

          A Offline
          A Offline
          AspDotNetDev
          wrote on last edited by
          #4

          Notice that it's a web method. If somebody tries to call the web method via a web service, an exception will be thrown, but it can't be caught in that method. There may be another place to catch such errors on the server-side (before the client gets an exception), but I'm not sure.

          Somebody in an online forum wrote:

          INTJs never really joke. They make a point. The joke is just a gift wrapper.

          S 1 Reply Last reply
          0
          • A AspDotNetDev

            Notice that it's a web method. If somebody tries to call the web method via a web service, an exception will be thrown, but it can't be caught in that method. There may be another place to catch such errors on the server-side (before the client gets an exception), but I'm not sure.

            Somebody in an online forum wrote:

            INTJs never really joke. They make a point. The joke is just a gift wrapper.

            S Offline
            S Offline
            SledgeHammer01
            wrote on last edited by
            #5

            You mean if somebody builds their own raw XML and calls the method directly? If you use a generated proxy class and its "out of date", the proxy class will handle the exception. Not quite sure what you are trying to accomplish.

            A 1 Reply Last reply
            0
            • S SledgeHammer01

              You mean if somebody builds their own raw XML and calls the method directly? If you use a generated proxy class and its "out of date", the proxy class will handle the exception. Not quite sure what you are trying to accomplish.

              A Offline
              A Offline
              AspDotNetDev
              wrote on last edited by
              #6

              I'm not trying to accomplish anything. I was just clarifying what the OP seems to be trying to get at. Seems like they want to have control over what is returned in the case that a bad request is received (e.g., return a message that details which parameters were missing from the call).

              Somebody in an online forum wrote:

              INTJs never really joke. They make a point. The joke is just a gift wrapper.

              1 Reply Last reply
              0
              • S si_69

                Hi I have a web method (see below) which accepts an integer as a parameter. I want to be able to catch the error if a paramter is not provided. I thought using catch (ArgumentException) would do the job Is there anything else i should be doing to be able to catch errors like this ? thanks Simon

                \[WebMethod\]
                public DataSet GetOrganisation(int organisationId)
                {
                
                    DataSet ds = new DataSet();
                    try
                    {
                

                .....

                    }
                    catch (MySql.Data.MySqlClient.MySqlException ex)
                    {
                        WriteToLog(organisationId.ToString(), "", "GetOrganisation", ex.Message);
                    }
                    catch (ArgumentException argerr)
                    {
                        WriteToLog(organisationId.ToString(), "", "GetOrganisation", argerr.Message);
                    }
                
                   
                    return ds;
                }
                
                E Offline
                E Offline
                Ennis Ray Lynch Jr
                wrote on last edited by
                #7

                Normally, in .NET I create wrapper classes for all of my web methods.

                [WebMethod]
                public FooBarResult FooBar(FooBarParameters parameters){ //FooBarParameters only if a lot
                FooBarResult result = new FooBarResult();
                try{
                ... do work here
                }
                catch(Exception e1){
                ... Perform logic to determine how much of the exception you can return and
                ... assign that to the result object in a special errors property
                }
                //Sometimes I will add misc. information like run time to the result here
                }

                While not perfect in any manner it lets me consistently write web methods and always know how the results are coming back. I do a lot of cross platform WS work which is a real pain because both the Java tools and the .NET tool behave differently. This pattern gives a small semblance of sanity.

                Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                1 Reply Last reply
                0
                • S si_69

                  Hi I have a web method (see below) which accepts an integer as a parameter. I want to be able to catch the error if a paramter is not provided. I thought using catch (ArgumentException) would do the job Is there anything else i should be doing to be able to catch errors like this ? thanks Simon

                  \[WebMethod\]
                  public DataSet GetOrganisation(int organisationId)
                  {
                  
                      DataSet ds = new DataSet();
                      try
                      {
                  

                  .....

                      }
                      catch (MySql.Data.MySqlClient.MySqlException ex)
                      {
                          WriteToLog(organisationId.ToString(), "", "GetOrganisation", ex.Message);
                      }
                      catch (ArgumentException argerr)
                      {
                          WriteToLog(organisationId.ToString(), "", "GetOrganisation", argerr.Message);
                      }
                  
                     
                      return ds;
                  }
                  
                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #8

                  Question doesn't make any sense. GetOrganisation() will not be invoke unless there is a value for organisationId. So there is no way to test in that method whether a client called it with a value or not. If you are asking about client code that calls that method then a much more likely error scenario is a communication fault. Although it is possible to call the server such that it is unable to resolve the method that is unlikely to be a valid scenario and it isn't a scenario that will exist for long. And even then it is a problem with the client code.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups