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. EXCEPTION HANDLING

EXCEPTION HANDLING

Scheduled Pinned Locked Moved C#
help
4 Posts 4 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.
  • M Offline
    M Offline
    mukundkallapur
    wrote on last edited by
    #1

    Kindly check the below program.

    /* Brief Decsription : This program checks whether the entered chatacter is a numeral or no.
    I have used the IsDigit Method. The issue here is that, the input from keyboard is a string,
    whereas the IsDigit method needs a character. Hence I have converted the string input to a character.

    Now, when I enter any number from 0 to 9, it perfoms the functionality of addition and also if I
    enter any character it hroes an Exception. But if I enter any 2 digit number, then also it throws an
    Exception, which is system generated.

    This is because of that converison to character.

    Kindly provide a solution wherein I can enter numerals greater than 9 without error */

    using System;

    namespsace TestException3
    {
    Class program
    {
    static void Main(string args[])
    {
    try
    {
    string s1,s2,ans;
    char type,type1;

    Console.WriteLine("please enter the variables : ");
    s1=Console.ReadLine();
    s2=Console.ReadLine();

    type=Convert.ToChar(s1);
    type1=Convert.ToChar(s2);

    if(!(char.IsDigit(type)) || !(char.IsDigit(type1)))
    {
    throw new Exception("Please Enter numbers only");
    }
    else
    {
    ans=Convert.ToString(Convert.ToInt32(s1)+Convert.ToInt32(s2));
    Console.WriteLine(Answer : {0}",ans);
    Console.ReadLine();
    }
    catch(Exception e)
    {
    Console.WriteLine("Message : {0}", e.Message);
    Console.ReadLine();
    }
    }
    }
    }

    J B M 3 Replies Last reply
    0
    • M mukundkallapur

      Kindly check the below program.

      /* Brief Decsription : This program checks whether the entered chatacter is a numeral or no.
      I have used the IsDigit Method. The issue here is that, the input from keyboard is a string,
      whereas the IsDigit method needs a character. Hence I have converted the string input to a character.

      Now, when I enter any number from 0 to 9, it perfoms the functionality of addition and also if I
      enter any character it hroes an Exception. But if I enter any 2 digit number, then also it throws an
      Exception, which is system generated.

      This is because of that converison to character.

      Kindly provide a solution wherein I can enter numerals greater than 9 without error */

      using System;

      namespsace TestException3
      {
      Class program
      {
      static void Main(string args[])
      {
      try
      {
      string s1,s2,ans;
      char type,type1;

      Console.WriteLine("please enter the variables : ");
      s1=Console.ReadLine();
      s2=Console.ReadLine();

      type=Convert.ToChar(s1);
      type1=Convert.ToChar(s2);

      if(!(char.IsDigit(type)) || !(char.IsDigit(type1)))
      {
      throw new Exception("Please Enter numbers only");
      }
      else
      {
      ans=Convert.ToString(Convert.ToInt32(s1)+Convert.ToInt32(s2));
      Console.WriteLine(Answer : {0}",ans);
      Console.ReadLine();
      }
      catch(Exception e)
      {
      Console.WriteLine("Message : {0}", e.Message);
      Console.ReadLine();
      }
      }
      }
      }

      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #2

      Hint:

      string number = "1234567890";
      foreach (char c in number)
      {
      Console.WriteLine(c);
      }

      Another Hint:

      int iNum = 0;
      string strNum = "1234567890";
      if (int.TryParse(strNum, out iNum))
      {
      Console.WriteLine(iNum);
      }

      :badger:

      1 Reply Last reply
      0
      • M mukundkallapur

        Kindly check the below program.

        /* Brief Decsription : This program checks whether the entered chatacter is a numeral or no.
        I have used the IsDigit Method. The issue here is that, the input from keyboard is a string,
        whereas the IsDigit method needs a character. Hence I have converted the string input to a character.

        Now, when I enter any number from 0 to 9, it perfoms the functionality of addition and also if I
        enter any character it hroes an Exception. But if I enter any 2 digit number, then also it throws an
        Exception, which is system generated.

        This is because of that converison to character.

        Kindly provide a solution wherein I can enter numerals greater than 9 without error */

        using System;

        namespsace TestException3
        {
        Class program
        {
        static void Main(string args[])
        {
        try
        {
        string s1,s2,ans;
        char type,type1;

        Console.WriteLine("please enter the variables : ");
        s1=Console.ReadLine();
        s2=Console.ReadLine();

        type=Convert.ToChar(s1);
        type1=Convert.ToChar(s2);

        if(!(char.IsDigit(type)) || !(char.IsDigit(type1)))
        {
        throw new Exception("Please Enter numbers only");
        }
        else
        {
        ans=Convert.ToString(Convert.ToInt32(s1)+Convert.ToInt32(s2));
        Console.WriteLine(Answer : {0}",ans);
        Console.ReadLine();
        }
        catch(Exception e)
        {
        Console.WriteLine("Message : {0}", e.Message);
        Console.ReadLine();
        }
        }
        }
        }

        B Offline
        B Offline
        Bardy85
        wrote on last edited by
        #3

        You could try the following.

        ConsoleKeyInfo ck = Console.ReadKey();
        char Key = ck.KeyChar;

        1 Reply Last reply
        0
        • M mukundkallapur

          Kindly check the below program.

          /* Brief Decsription : This program checks whether the entered chatacter is a numeral or no.
          I have used the IsDigit Method. The issue here is that, the input from keyboard is a string,
          whereas the IsDigit method needs a character. Hence I have converted the string input to a character.

          Now, when I enter any number from 0 to 9, it perfoms the functionality of addition and also if I
          enter any character it hroes an Exception. But if I enter any 2 digit number, then also it throws an
          Exception, which is system generated.

          This is because of that converison to character.

          Kindly provide a solution wherein I can enter numerals greater than 9 without error */

          using System;

          namespsace TestException3
          {
          Class program
          {
          static void Main(string args[])
          {
          try
          {
          string s1,s2,ans;
          char type,type1;

          Console.WriteLine("please enter the variables : ");
          s1=Console.ReadLine();
          s2=Console.ReadLine();

          type=Convert.ToChar(s1);
          type1=Convert.ToChar(s2);

          if(!(char.IsDigit(type)) || !(char.IsDigit(type1)))
          {
          throw new Exception("Please Enter numbers only");
          }
          else
          {
          ans=Convert.ToString(Convert.ToInt32(s1)+Convert.ToInt32(s2));
          Console.WriteLine(Answer : {0}",ans);
          Console.ReadLine();
          }
          catch(Exception e)
          {
          Console.WriteLine("Message : {0}", e.Message);
          Console.ReadLine();
          }
          }
          }
          }

          M Offline
          M Offline
          Migounette
          wrote on last edited by
          #4

          Try this: public void test() { try { Console.WriteLine("Please enter the variables : "); Int32 valueOne = ConvertLine(Console.ReadLine()); Int32 valueTwo = ConvertLine(Console.ReadLine()); Console.WriteLine("Answer : {0}", valueOne + valueTwo); Console.ReadLine(); } catch (Exception e) { Console.WriteLine("Message : {0}", e.Message); Console.ReadLine(); } } private int ConvertLine(string value) { try { if (String.IsNullOrEmpty(value) == false) { return Convert.ToInt32(value); } Console.Error.WriteLine("Empty field is not supported : default value used 0"); } catch (Exception exception) { Console.Error.WriteLine(String.Format("{0} : default value used 0", exception.Message)); } return 0; }

          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