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. try catch in C#

try catch in C#

Scheduled Pinned Locked Moved C#
questioncsharphelptutorial
8 Posts 7 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.
  • J Offline
    J Offline
    jojoba2011
    wrote on last edited by
    #1

    How can i get : the value or Parameter that cause error in try block? Simple example : try { int i=0; int j=2; int k=j/i; } catch(Exception e) { messagebox.show(e.Message); } now i wanna to have these : in line 5 the exception occurred and i=0 made the exception;

    N D J P C 5 Replies Last reply
    0
    • J jojoba2011

      How can i get : the value or Parameter that cause error in try block? Simple example : try { int i=0; int j=2; int k=j/i; } catch(Exception e) { messagebox.show(e.Message); } now i wanna to have these : in line 5 the exception occurred and i=0 made the exception;

      N Offline
      N Offline
      Nematjon Rahmanov
      wrote on last edited by
      #2

      you mean ,

      try
      {
      int i=0;
      int j=2;
      if(i==0)
      {
      throw new ArgumentNullException();// or your exception class
      }
      int k=j/i;
      }
      catch(Exception e)
      {
      messagebox.show(e.Message);
      }

      We are haven't bug,just temporarily undecided problems.

      1 Reply Last reply
      0
      • J jojoba2011

        How can i get : the value or Parameter that cause error in try block? Simple example : try { int i=0; int j=2; int k=j/i; } catch(Exception e) { messagebox.show(e.Message); } now i wanna to have these : in line 5 the exception occurred and i=0 made the exception;

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        jojoba2011 wrote:

        the value or Parameter that cause error in try block?

        If you're asking what I think you are, you can't get that detailed of a message.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008
        But no longer in 2009...

        1 Reply Last reply
        0
        • J jojoba2011

          How can i get : the value or Parameter that cause error in try block? Simple example : try { int i=0; int j=2; int k=j/i; } catch(Exception e) { messagebox.show(e.Message); } now i wanna to have these : in line 5 the exception occurred and i=0 made the exception;

          J Offline
          J Offline
          jojoba2011
          wrote on last edited by
          #4

          I exactly want this message :

          line 5 exception occured
          i=0 made this error

          N L 2 Replies Last reply
          0
          • J jojoba2011

            I exactly want this message :

            line 5 exception occured
            i=0 made this error

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            Stack trace will have these details. You have to parse the stack trace and extract this information.

            Best wishes, Navaneeth

            1 Reply Last reply
            0
            • J jojoba2011

              I exactly want this message :

              line 5 exception occured
              i=0 made this error

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Move i outside the try/catch block so it is visible in all sections, something like:

              int i;
              try
              {
              i=0;
              int j=2;
              int k=j/i;
              }
              catch(Exception e)
              {
              string msg = "i = " + i + " " + e.Message;
              messagebox.show(msg);
              }

              txtspeak is the realm of 9 year old children, not developers. Christian Graus

              1 Reply Last reply
              0
              • J jojoba2011

                How can i get : the value or Parameter that cause error in try block? Simple example : try { int i=0; int j=2; int k=j/i; } catch(Exception e) { messagebox.show(e.Message); } now i wanna to have these : in line 5 the exception occurred and i=0 made the exception;

                P Online
                P Online
                PIEBALDconsult
                wrote on last edited by
                #7

                i=0 didn't cause the Exception; it's j/i. You could put the three statements each in its own try/catch. You could keep track of where you were in the process:

                int x;

                try
                {
                x=0;
                int i=0;

                x=1;
                int j=2;

                x=2;
                int k=j/i;

                x=3;
                }
                catch(Exception e)
                {
                string err="";

                switch(x)
                {
                case 0: err = " caused by i=0" ; break ;
                case 1: err = " caused by j=2" ; break ;
                case 2: err = " caused by k=j/i" ; break ;
                }

                messagebox.show(e.Message + err );
                }

                1 Reply Last reply
                0
                • J jojoba2011

                  How can i get : the value or Parameter that cause error in try block? Simple example : try { int i=0; int j=2; int k=j/i; } catch(Exception e) { messagebox.show(e.Message); } now i wanna to have these : in line 5 the exception occurred and i=0 made the exception;

                  C Offline
                  C Offline
                  carlecomm
                  wrote on last edited by
                  #8

                  "i=0 made the exception" can be thrown by catch(DevideByZeroException) { } but I don't know how to throw "in line 5 the exception occurred" until now.

                  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