c# - nicely exception handling -


in our app, use components developed other teams. question how can define nicely way of exception handling this

   try    {     somecomponent.dostuff();    }     catch (exception ex)    {     textlabel= ex.message;    } 

the component has no custom exception type, maybe nicely way define component specific exception type , wrap somehow? know question basic, interested more in let's how it. if call component no custom defined exception types, how handle potential exceptions in elegant way?

when catch error able repackage , throw error, @ basic level may adding more data - but, you've suggested, replace generic error custom error that, whilst won't overcome limitations of response you've got component, give code further call stack opportunity respond more appropriately.

so in terms of adding information in basic manner - throwing new exception additional text whilst still passing original exception:

catch (exception ex) {     throw new exception("this more exception occurred", ex); } 

now, if want define own custom component exception change new exception new componentspecificexception adding data necessary constructor never forgetting set inner exception. exceptions have data collection of key, value pairs can insert more information (by creating exception, adding data , doing throw).

that's generic - working forward there, can't anticipate exceptions have handle don't try - set logging know when you've got generic exception i.e. 1 hits final catch - , on time add exception specific catches above generic provide more appropriate responses or, @ least, package error less general custom exceptions.

not sure i've explained - notion difficult anticipate every possible error want have strategy develop application in systematic fashion discover new exceptions.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -