Try Catch , is it enough to prevent crash and error messages ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Try Catch , is it enough to prevent crash and error messages ?

  1. #1
    Join Date
    Jun 2009
    Posts
    144

    Try Catch , is it enough to prevent crash and error messages ?

    Hello i want to make a simple program that will collect some stats from a local database and then it will email them every 30 minutes to my email address.

    I want to be sure that the program will run without crashing so i believe Try Catch is what i want but i dont know if im right.

    For example if the pc is not connected to the internet and my program cant communicate with mail.server.com the program will crash , but if i use Try Catch it will execute the Catch code.

    Is there any need to have nested Try Catch or one is enough for multiple functions ? For example

    Code:
    Try {
    
    function1();
    function2();
    
    }
    It will handle the two functions ? or i have to put Try Catch inside functions like this

    Code:
    void function1() {
    
    Try { } 
    
    }
    Thanks !

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,451

    Re: Try Catch , is it enough to prevent crash and error messages ?

    You really need to think about what you want to do. For example. In you first bit of code, if an error occurs in "function1" then function 2 gets skipped. Thinking about it using your e-mail reference above, if Function1 gets the data from the database, and function2 EMails you, then if function1 fails, you won't get any email. To use you limited example, you should do something like...

    Code:
    Try {
        GetData();
        EMailData();
    }
    catch
    {
        LogError();
    }
    So it is not just using try/catch to keep the program running. Think about the possible errors that might occur, and use the try/catch to manage those errors and take appropriate action.

  3. #3
    Join Date
    Jun 2009
    Posts
    144

    Re: Try Catch , is it enough to prevent crash and error messages ?

    thanks for the answer , its more about the errors not about the functions that will be skipped. I want no error messages and no crashes , i want no user interaction at all ! if use :

    Code:
    try { 
    
    function1();
    function2();
    
    //some other code here which migth crash the application
    
    }
    the "Try Catch" will absorp the function1 and function2 errors or it will handle only errors at code after the 2 functions ?

    What i want to know is , if function1 is called within a try catch is it necessary to include try catch inside function1 ?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,485

    Re: Try Catch , is it enough to prevent crash and error messages ?

    Quote Originally Posted by invader7 View Post
    the "Try Catch" will absorp the function1 and function2 errors or it will handle only errors at code after the 2 functions ?

    What i want to know is , if function1 is called within a try catch is it necessary to include try catch inside function1 ?
    Why not write some test code and try the questions out? I mean I could answer your questions, but then it would probably lead to confusion and more questions (and I don't mean any disrespect when I say that to you).

    Try the following code, play around with it and understand how try/catch blocks work and the nesting of try/catch blocks work.

    Code:
    private static void Main()
    {
      try
      {
        Function1();
        Function2();
    
        // throw new Exception("Exception thrown in main function");
      }
      catch( Exception ex )
      {
         Console.WriteLine(String.Format("Exception handled in Main: {0}", ex.Message));
      }
    }
    private void Function1()
    {
      Console.WriteLine("In function 1");
    
      throw new Exception("Exception thrown in function 1");  // NOTE: after running the program once, comment out this line
    
      Console.WriteLine("Out function 1");
    
    }
    
    private void Function2()
    {
      Console.WriteLine("In function 2");
    
      try
      {
        // throw new Exception("Exception 1 thrown in function 2");  // Note: after running this program a 3rd time, comment this line
      }
      catch( Exception ex)
      {
       Console.WriteLine(String.Format("Exception handled in Function 2: {0}", ex.Message));
      }
    
      //  throw new Exception("Exception 2 thrown in function 2");  // Note: after running this program the second time, comment out this line
    
    
      Console.WriteLine("Out function 2");
    }
    Last edited by Arjay; January 31st, 2013 at 01:55 PM.

  5. #5
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,006

    Re: Try Catch , is it enough to prevent crash and error messages ?

    In some sense, the worst possible outcome is that your application crashes entirely silently. You should always have SOME sort of reporting when errors occur, otherwise it will be very difficult to find errors if your program is ever running in a more complex environment. Such reporting doesn't need to interrupt a user though. Minimally, it should output an error code than can be read on the console. You can do this by dying from the catch with:
    Code:
    try
    {
        //Code here
    }
    catch
    {
        //Some error processing
    
        //And then, Terminate program, with error code 1, indicating abnormal termination
        Environment.Exit(1);
    }
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Jun 2009
    Posts
    144

    Re: Try Catch , is it enough to prevent crash and error messages ?

    Thank you allo very much for your time ! Arjay thanks for the great example !

  7. #7
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    51

    Re: Try Catch , is it enough to prevent crash and error messages ?

    In my opinion, interpretation, is better than risk and avoiding. You shouldn't just throw away your brain and let a try catch block do everything for you to hide errors, unless you have something to do when a specific error is caught. Otherwise, prevention is much better, both for application design, and performance. Why are you expecting your program to possibly crash? What would make it crash? Try to implement barricades that would prevent as much as that as possible from happening. Using a try catch to just catch System.Exception is really bad if it can be avoided.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center