c# - Exception Handling UWP -
i started write first program uwp (universal windows platform). c# , read try-catch block exceptions.
have special structure in app , want advice best kind of exception handling.
my setup follows:
have page called mainpage method getinformation.
have 2 classes, named getsetting , getconnection. getinformation of mainpage input user , send them getconnections. in method call getsetting (nested method).
this structure. know error can happen in every part: user inputs not valid, or can't connection form systems, maybe can't access settings files, or other errors. i've added try-catch blocks each part of app (so in getinformation, getconnection, getsetting).
- i read message dialog asynchronously method: if try add message dialog each part of application if there error in each 1 3 message dialog errors. don't want make app unclear adding task , await methods because 1 part of app. couldn't find way stop in application , force wait user input. stopped using way.
- i tried second way: add control in mainpage show error message. gives me problem: there no way return nested methods (i looked 1 didn't find it). inside getconnection , getsetting have no access ui elements. have throw exception inside of try-catch block again until mainpage. approach can't show special information exception. want keep exception , don't want make new exception , throw mainpage becasue stacktrace readonly can't set new exception throw exception created app , can't add special information.
these ways , both have problems. best way exception handling in situation.
update:
public string getinformation() { // codes here var data = getconnection(); // code here use data // other codes } public string getconnection() // in class connection { // codes here var data = getsetting(); // code here use data // other codes } public string getsetting() // in class setting { // codes here }
this code. in every part of codes "some codes here" can error happen. best way?
you can return when catch exception , display message
public void get() { //your code string returnvalue; try { returnvalue = getinformation(); } catch (exception) { return; } //code not cause exception try { //another possible line of causing exception } catch (exception) { //error message return; } //codes not cause exception } public string getinformation() { try { //your code } catch (exception e) { //error message throw new exception(); } }
Comments
Post a Comment