Run program within Try / Catch

7

Working with Visual Studio, when an error occurs while the program is running the program crashes and you can not see where the program crashed and only that error screen appeared.

To get where these errors occur we use try catch , so we can capture where and why the error happened.

One possibility to never have this problem is to put the entire program within try catch , but I know it is not a good practice.

class Program
{
    static void Main(string[] args)
    {
        try
        {
            //Todo o programa
        }
        catch (Exception ex)
        {
            //Capturar os erros
        }
    }
}

What problems or why not leave the entire program within try catch ?

    
asked by anonymous 06.03.2014 / 13:24

3 answers

6

As you can see by reading the end of this article , the two main arguments are performance and integrity .

Performance

The first and most obvious reason for not using a try/catch in the entire program is that, generating a exception is much more costly than controlling the work flow of the program with if/else . In case no exception is thrown, if/else actually slows down, if a single exception is thrown, the execution time spent treating this exception is equivalent to several if/else blocks.

Integrity

The second reason, a little less obvious, is that if you've done anything before the exception is thrown, everything that happens until you reach the exception gets executed, which makes execution part of the useless code. In the meantime, in% w /% these lines that would be useless are ignored before they are executed.

Other issues

In addition to these two problems, in this answer he still addresses the semantic question. Exceptions are for exceptional cases, so they should be used when you do not wait for an error to happen there. While if/else should be used to control the flow of the program, making it even more natural to read and keep the code easier.

A good example of when we should use if/else is when we are trying to load a file, faults outside your program may occur, such as that the file no longer exists or there is a problem reading the storage drive, in those cases where success is expected and you can not predict the error is where try/catch should be used.

    
06.03.2014 / 13:41
5

No. This is not the way to do this.

There are hexes to put the entire program inside a try catch block but I will not refer to them but will show you the right way to do it.

There is a way on the .NET platform to register a piece of code that will be fired whenever an exception is not handled and reaches the top of the unmanaged exchanger stack.

The following code, extracted from a question in stackoverflow .com, exemplifies usage:

using System;

class Program {
    static void Main(string[] args) {
        AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
        throw new Exception("Kaboom");
    }

    static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e) {
        Console.WriteLine(e.ExceptionObject.ToString());
        Console.WriteLine("Press Enter to continue");
        Console.ReadLine();
        Environment.Exit(1);
    }
}

The code you register will play your catch clause of a try that involves the application as a whole.

    
06.03.2014 / 13:35
3

To prevent all code from being within a try / catch you should hone your tests to detect failures. Everything must be validated following good programming techniques.
So data entries must be validated, system calls, function returns access rights to resources, etc ...
Applying best practices can be a great help.
Some tools can be seen here: Code Coverage for C # / .NET

    
06.03.2014 / 14:33