Wrap each method in a try-catch or specific piece of code

My senior colleague tells me to wrap each method in a try-catch block so that they can track where exceptions occur in order to debug problems faster. Is it better to wrap each method in Try Catch, for example:

Public int foo() { try { //do something }catch(Exeception ex) { //do something with ex } } 

Or is it better to catch exceptions where I think they can happen? For example. something with an array can IndexOutOfRangeException .

 //wrap this in try catch int[] array = new int[3]; array[0] = 1; array[1] = 2; array[2] = 3; array[3] = 4; 

Thanks.

+7
c # try-catch
source share
5 answers

The try block contains protected code that may throw an exception. The block is executed until an exception is thrown or it is successfully completed.

You can take a look at How often should I use try and catch

The basic rule for catching exceptions is to catch exceptions if and only if you have a meaningful way to handle them.

Do not catch the exception if you are going to throw an exception and throw it on the stack. It does not contain any meaning and does not put code.

Catch an exception if you expect a failure in a certain part of your code, and if you have a reserve for it.

Of course, you always deal with checked exceptions that require the use of try / catch blocks, in which case you have no other choice. Even with a proven exception, make sure that you record and process correctly as clean as possible.

+4
source share

It is better to use it in critical parts of your code, and then:

 [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); ServerForm form = new ServerForm(); Application.Run(form); } static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { MessageBox.Show(e.Exception.Message, Program.Name); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { MessageBox.Show((e.ExceptionObject as Exception).Message, Program.Name); } 

only in case of an unhandled exception

0
source share

There is no problem using Try catch block as it has no overhead (unless you add it everywhere, which may include overhead in readability), while an extra IL for catch is often added, and finally blocks, when an exception is not thrown in the behavior there are few differences.

However, if your code throws an exception, there is a slight performance issue here, since in such cases an exception must be thrown, stack bypass labels should be placed, and if the exception is handled and its StackTrace property is available, it should be stacked. therefore, as a result, it may not be entirely advisable to always wrap your code in a catch catch block , otherwise you can place it at the parent level and then check the stack trace

0
source share

Using try-catch is highly context sensitive. There are no rules for me to tell the developer when you are using or not using the catch try block.

Developer code should prevent obvious errors or exceptions due to context, such as null parameters or file existence or data consistency. In the case of developing a library used by many other programs, the library catches only some critical exception, so that top-level programs have more detailed information about errors and exceptions.

For example, we have a method in a library using System.IO.File.WriteAllLines.

 void bool DoSomethingWithFile() { try { // Some code here System.IO.File.WriteAllLines() //some code here return true; } catch() { LogExeption(); return false; } } 

How to tell a top-level program that a PathTooLongException is thrown or a security exception if you don't add a throw to the catch block.

0
source share

Each piece of code in which you think an error may occur must be cracked inside the catch try block. If you are working on some problems and applications in real time, you should use them everywhere. This is a good programming practice. If you do not know what exception might occur, just use the catch block for general exceptions:

 try { //your code } catch (Exception ex) { //exception handling } 

Or you could use:

 try { //your code } catch { //your custom message } 
-2
source share

All Articles