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. Need help with handling exception

Need help with handling exception

Scheduled Pinned Locked Moved C#
questionsysadminhelptutorialworkspace
8 Posts 2 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.
  • T Offline
    T Offline
    TheJudeDude
    wrote on last edited by
    #1

    I have this assignment m_strFileName = OpenFile(); in which there is a try/catch block within the OpenFile method. private string OpenFile() { string strFilename; if (intStoreNumber == 201) { strFilename = "t1"; } else if (intStoreNumber == 202) { strFilename = "t2"; } else if (intStoreNumber == 203) { strFilename = "T3"; } else { strFilename = intStoreNumber.ToString(); strFilename = strFilename.PadLeft(2,'0'); } strFilename = "afford" + strFilename + ".dat"; try { File.Copy("F:\\BORIS\\" + strFilename, Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + strFilename,true); } catch { MessageBox.Show("The file " + strFilename + " can not be found. Please check " + "that you selected the correct store and that your network connection to" + " drive F: is not disconnected","File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } strFilename = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + strFilename; return strFilename; The question I have is if the program runs into this exception, how do I return it back to waiting on an event versus going through to the next method? Everything I got from google tells me all about the structure of a try/catch/finally block, but nothing much more on how to exit gracefully without shutting down the program. Thanx in advance.

    Jude

    A 1 Reply Last reply
    0
    • T TheJudeDude

      I have this assignment m_strFileName = OpenFile(); in which there is a try/catch block within the OpenFile method. private string OpenFile() { string strFilename; if (intStoreNumber == 201) { strFilename = "t1"; } else if (intStoreNumber == 202) { strFilename = "t2"; } else if (intStoreNumber == 203) { strFilename = "T3"; } else { strFilename = intStoreNumber.ToString(); strFilename = strFilename.PadLeft(2,'0'); } strFilename = "afford" + strFilename + ".dat"; try { File.Copy("F:\\BORIS\\" + strFilename, Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + strFilename,true); } catch { MessageBox.Show("The file " + strFilename + " can not be found. Please check " + "that you selected the correct store and that your network connection to" + " drive F: is not disconnected","File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } strFilename = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + strFilename; return strFilename; The question I have is if the program runs into this exception, how do I return it back to waiting on an event versus going through to the next method? Everything I got from google tells me all about the structure of a try/catch/finally block, but nothing much more on how to exit gracefully without shutting down the program. Thanx in advance.

      Jude

      A Offline
      A Offline
      AB7771
      wrote on last edited by
      #2

      why would your program shut down? you have handled the exception and displayed a message box, the user will click the ok button on the message box and the program will continue further. Can you please elaborate your problem? what actually do u wanna do?

      Thanks & Regards, Pramod "Everyone is a genius at least once a year"

      T 1 Reply Last reply
      0
      • A AB7771

        why would your program shut down? you have handled the exception and displayed a message box, the user will click the ok button on the message box and the program will continue further. Can you please elaborate your problem? what actually do u wanna do?

        Thanks & Regards, Pramod "Everyone is a genius at least once a year"

        T Offline
        T Offline
        TheJudeDude
        wrote on last edited by
        #3

        I do not want the program to shut down. The method that is called returns a value that must be used in the next step of the program. But since the exception was thrown, I do not want the program to progress to the next called method. I want it to kill the forward progress and have the app wait for an event. Thanx for the reply!

        Jude

        A 1 Reply Last reply
        0
        • T TheJudeDude

          I do not want the program to shut down. The method that is called returns a value that must be used in the next step of the program. But since the exception was thrown, I do not want the program to progress to the next called method. I want it to kill the forward progress and have the app wait for an event. Thanx for the reply!

          Jude

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

          jsut make the following changes try { File.Copy("F:\\BORIS\\" + strFilename, Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + strFilename,true); strFilename = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + strFilename; } catch { MessageBox.Show("The file " + strFilename + " can not be found. Please check " + "that you selected the correct store and that your network connection to" + " drive F: is not disconnected","File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); strFilename = string.Empty; } return strFilename; and when u call the method OpenFile() just check if the return string is Empty or not if it is empty string dont do any thing else process furhter. Hope that is what you want to do...

          Thanks & Regards, Pramod "Everyone is a genius at least once a year"

          T 1 Reply Last reply
          0
          • A AB7771

            jsut make the following changes try { File.Copy("F:\\BORIS\\" + strFilename, Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + strFilename,true); strFilename = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + strFilename; } catch { MessageBox.Show("The file " + strFilename + " can not be found. Please check " + "that you selected the correct store and that your network connection to" + " drive F: is not disconnected","File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); strFilename = string.Empty; } return strFilename; and when u call the method OpenFile() just check if the return string is Empty or not if it is empty string dont do any thing else process furhter. Hope that is what you want to do...

            Thanks & Regards, Pramod "Everyone is a genius at least once a year"

            T Offline
            T Offline
            TheJudeDude
            wrote on last edited by
            #5

            Thanx for the reply....but how do I stop the execution of the code? It is withing a button's click event. I would supply the code, but it is on my work desktop and I am at home. break would not work because it is not within a loop, etc

            Jude

            T A 2 Replies Last reply
            0
            • T TheJudeDude

              Thanx for the reply....but how do I stop the execution of the code? It is withing a button's click event. I would supply the code, but it is on my work desktop and I am at home. break would not work because it is not within a loop, etc

              Jude

              T Offline
              T Offline
              TheJudeDude
              wrote on last edited by
              #6

              nevermind...brainfart!

              Jude

              1 Reply Last reply
              0
              • T TheJudeDude

                Thanx for the reply....but how do I stop the execution of the code? It is withing a button's click event. I would supply the code, but it is on my work desktop and I am at home. break would not work because it is not within a loop, etc

                Jude

                A Offline
                A Offline
                AB7771
                wrote on last edited by
                #7

                what do u mean by stop the execution of the code. You just have to write the code in the IF block,,, if the string is not empty perform what ever you want else nothing... e.g. if(!String.Equals(String.Empty, Openfile()) { ///DO WHAT U WANT DO } else nothing will happen.... Is that fine?

                Thanks & Regards, Pramod "Everyone is a genius at least once a year"

                T 1 Reply Last reply
                0
                • A AB7771

                  what do u mean by stop the execution of the code. You just have to write the code in the IF block,,, if the string is not empty perform what ever you want else nothing... e.g. if(!String.Equals(String.Empty, Openfile()) { ///DO WHAT U WANT DO } else nothing will happen.... Is that fine?

                  Thanks & Regards, Pramod "Everyone is a genius at least once a year"

                  T Offline
                  T Offline
                  TheJudeDude
                  wrote on last edited by
                  #8

                  yes, read previous post and forgive my brainfart!

                  Jude

                  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