0
votes

I started to write my first program for UWP (Universal Windows Platform). I like C# and I read about try-catch block to get exceptions.
I have a special structure in my app and I want to get some advice for the best kind of exception handling.

My setup is as follows:
I have a page called MainPage with a method GetInformation.
I have two classes, named GetSetting and GetConnection. With GetInformation of the MainPage I get input from the user and send them to GetConnections. In that method I then call GetSetting (nested method).

This is my structure. Now I know that an error can happen in every part: the user inputs not valid, or I can't get a connection to the form systems, maybe can't access settings files, or some other errors. So I've added try-catch blocks to each part of my app (so in GetInformation, GetConnection, GetSetting).

  • I read about Message Dialog but it is an asynchronously method: If I try to add a message dialog to each part of the application but if there is an error in each one then I get 3 message dialog for my errors. I don't want to make my app unclear by adding task and await to all my methods because it is only one part of my app. I couldn't find any way for stop in my application and force to wait for user input. So I stopped using this way.
  • I tried a second way: I add a control in my MainPage to show an error message. This gives me another problem: There is no way to return from all nested methods (I looked for one but I didn't find it). But inside GetConnection and GetSetting I have no access to the UI elements. So I have to throw the exception inside of my try-catch block again until I get to the MainPage. But with this approach I can't show any special information about that exception. I want to keep my exception and I don't want to make new exception and throw that to MainPage becasue StackTrace is readonly so I can't set it for my new exception so I only throw the exception that is created by app and I can't add any special information.

These are my ways and both have some problems. What is the best way for exception handling in this situation.

Update:

public string GetInformation()
{
    // Some codes here
    var data = GetConnection();
    // Some code here that use data
    // Some other codes
}
public string GetConnection() // In class Connection
{
    // Some codes here
    var data = GetSetting();
    // Some code here that use data
    // Some other codes
}
public string GetSetting() // in class Setting
{
    // Some codes here
}

This is my code. in every part of codes like "some codes here" can error happen. What is the best way?

2
What do you want to do when you get an exception? - Vijay Nirmal
I want to show information such the place of method that error happened like myApp.GetInformation and some other information then a button for send report to me. - Bor
So you want to stop the further execution of the GetInformation() method when you got an error. Am I right? - Vijay Nirmal
Yes. exactly this is my problem. - Bor

2 Answers

-2
votes

You can return when you catch an Exception and Display your message

public void Get()
{
    //Your code

    string returnValue;
    try
    {
        returnValue = GetInformation();
    }
    catch (Exception)
    {
        return;
    }

    //Code that will not cause exception

    try
    {
        //Another possible line of causing exception
    }
    catch (Exception)
    {
        //Error Message
        return;
    }

    //Codes that will not cause exception
}

public string GetInformation()
{
    try
    {
        //Your Code
    }
    catch (Exception e)
    {
        //Error message
        throw new Exception();
    }
}
0
votes

You need to add a Validation() method in your final step to get errors, if any, could occurred or not.

GetInformation()
{
  return information;  
}

GetConnection()
{
    return connection;
}
GetSetting()
{
    return setting;
}

Now in your MainPage include the Validation() method and implement your logic to show the results in one messageDialog()

Hope this helps.