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. Loop through Error

Loop through Error

Scheduled Pinned Locked Moved C#
questionhelp
6 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.
  • V Offline
    V Offline
    Vector7
    wrote on last edited by
    #1

    try { some code } catch(Exception ex) { some code } How can I loop with "forEach" statement through all exceptions ?

    S D 2 Replies Last reply
    0
    • V Vector7

      try { some code } catch(Exception ex) { some code } How can I loop with "forEach" statement through all exceptions ?

      S Offline
      S Offline
      spif2001
      wrote on last edited by
      #2

      ??? a catch block only catches one Exception. Why do u whant to loop and where? I don't understand your request spif2001

      V 1 Reply Last reply
      0
      • S spif2001

        ??? a catch block only catches one Exception. Why do u whant to loop and where? I don't understand your request spif2001

        V Offline
        V Offline
        Vector7
        wrote on last edited by
        #3

        Hi, Here is one example for SQLError try { some code } catch(SqlException ex) { foreach(SqlError sqlError in ex.Errors) { some code } } Sometimes you get more than one exeption at the same time ( especially SQL for example )

        1 Reply Last reply
        0
        • V Vector7

          try { some code } catch(Exception ex) { some code } How can I loop with "forEach" statement through all exceptions ?

          D Offline
          D Offline
          DavidNohejl
          wrote on last edited by
          #4

          hi, As was already mentioned, catch "catches" only one Exception. I think you are trying to do something like : try { //some code if(stg) { myBadFunctionThrowsMySpecificExcpetion(); } else { i= i/0; // throws another exception } } catch(MySpecificException ex) { // handle my specific exception } catch(Exception ex) { // handle generic exception (in this case divide by zero) } When exception is raised in try block, it is catched in "closest" catch if throwed exception is (in meaning that operator is would return true - same type or derived class) the same as declared in catch block. If exception of diferent type is thrown, then it passes thru first catch block to another (if present),.. etc. You must write catch blocks from specific to most generic; if you wrote catch for System.Excpetion first, all exceptions would be handled in this block. No other would have chance to trap error. I only hope this is answer to your question :) best regards, David 'DNH' Nohejl Never forget: "Stay kul and happy" (I.A.)

          V 1 Reply Last reply
          0
          • D DavidNohejl

            hi, As was already mentioned, catch "catches" only one Exception. I think you are trying to do something like : try { //some code if(stg) { myBadFunctionThrowsMySpecificExcpetion(); } else { i= i/0; // throws another exception } } catch(MySpecificException ex) { // handle my specific exception } catch(Exception ex) { // handle generic exception (in this case divide by zero) } When exception is raised in try block, it is catched in "closest" catch if throwed exception is (in meaning that operator is would return true - same type or derived class) the same as declared in catch block. If exception of diferent type is thrown, then it passes thru first catch block to another (if present),.. etc. You must write catch blocks from specific to most generic; if you wrote catch for System.Excpetion first, all exceptions would be handled in this block. No other would have chance to trap error. I only hope this is answer to your question :) best regards, David 'DNH' Nohejl Never forget: "Stay kul and happy" (I.A.)

            V Offline
            V Offline
            Vector7
            wrote on last edited by
            #5

            You are close to the problem, but i want get all error messages from all risen errors there. In the last message I have wrote an example for SQL tasks. Here it works, because you can assign a specific error, so I think it shpuld be possible to loop within all possible errors ( I need the name for the element and elements-> foreach(element in elements))

            J 1 Reply Last reply
            0
            • V Vector7

              You are close to the problem, but i want get all error messages from all risen errors there. In the last message I have wrote an example for SQL tasks. Here it works, because you can assign a specific error, so I think it shpuld be possible to loop within all possible errors ( I need the name for the element and elements-> foreach(element in elements))

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              how about: try { // something error prone } catch(Exception e) { Exception ex = e; while(ex != null) { Console.WriteLine(ex) ex = ex.InnerException; } }

              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