Can an exception be thrown

I have two methods I'm working on. One saves and one loads. Obviously, both require some kind of error handling, so I implemented some “tricks of all processing”. Now the heel of the hunt, what happens next, is contextual for where, at runtime, an error occurs. Because of this, I would like to handle the error in the caller, one level higher. Thus, I may have different logic for different situations.

An example is. If I check the load on the first run and it does not work, I can assume that their memory may be cleared. But if I try to load at runtime, I can assume that the memory was not cleared (by the right means), and there should be something.

public void SaveToStorage(AccountCollection Collection) { try { var storage = IsolatedStorageSettings.ApplicationSettings; storage["defaultCollection"] = Collection; storage.Save(); } catch (Exception ex) { // Do something meaningful here } } public AccountCollection LoadFromStorage() { try { AccountCollection collection; var storage = IsolatedStorageSettings.ApplicationSettings; storage.TryGetValue("defaultCollection", out collection); return collection; } catch (Exception ex) { // Do something meaningful here } return null; } 

Essentially, I ask if I can make the error to the caller, but still keep the original error information.

EDIT: Both John and Andrew gave the correct answers. Andrew will get a green checkmark as I would like to do some other general cleanings in the original class.

+4
source share
7 answers

In all the negative statements that they didn’t catch, he stated that he would like to unwind a little the method that throws an exception, but also continue it on the stack so that other methods can do something. He does not catch exceptions, just to throw it again, he wants to perform some action, and then throw it again.

 catch (Exception ex) { // Do something throw; } 

Edit

For some reason, I read this as C ++, so I deleted my comments on copying the exception.

+11
source

If you do not catch the exception, it will propagate up.

How it works. Do not put try / catch blocks everywhere. Just put them where you are going to handle the exception.

+6
source

If the only thing you want to do is rethink the exception, then it’s better not to even have a try / catch, as it will propagate upward, but if you want to do something and reverse it, you need this:

 catch(Exception ex) { // Do your bit with ex throw; } 

Using only throw , you save all the exception information, including the stack trace.

http://winterdom.com/2002/09/rethrowingexceptionsinc

+4
source

This MSDN example sounds like you're looking.

In this example, you will recognize the original exception, take the appropriate action (in this case, writing the line to the console), then create and throw a new exception containing the original.

 class TestTryCatch { static int GetInt(int[] array, int index) { try { return array[index]; } catch (System.IndexOutOfRangeException e) // CS0168 { System.Console.WriteLine(e.Message); //set IndexOutOfRangeException to the new exception InnerException throw new System.ArgumentOutOfRangeException("index parameter is out of range.", e); } } } 

http://msdn.microsoft.com/en-us/library/ms173165(v=vs.80).aspx

+2
source

This is usually achieved by wrapping the excluded binding in the new one using the InnerException property. This way you can send more meaningful details and the original exception to the caller:

 catch(Exception ex) { // Do something... throw new MyOwnDataStorageException("message", ex); } 

Or you can do just nothing, and the exception will automatically apply to the caller with a full call.

+1
source

If you cannot do anything useful when you cannot catch the atonement in AccountCollection (), let it go to the calling method and process it there.

0
source

You can simply throw the original exception (in your current code available as ex ) in your catch . It really depends on whether you want to handle exception handling in these functions. If you don't want this, put try / catch blocks outside (put them in the caller where you want to handle the exception (s)).

-2
source

Source: https://habr.com/ru/post/1334526/


All Articles