i have this:
public byte[] anymethod(){ try { ... } catch (exception e) { string errormessage = "some custom message, should caller of method should receive"; // thought of ,to pass through custom exception caller?! throw new applicationexception(errormessage); //but not allow method } }
but this:
throw new applicationexception(errormessage);
will result in:
an exception of type 'system.applicationexception' occurred in ...dll not handled in user code
how give custom errror message caller of above mentioned method ?
first, use custom exception or @ least 1 more meaningful instead of applicationexception
. second, have catch exception if method throws it.
so calling method should wrap method call in try...catch
:
try { byte[] result = anymethod(); }catch(mycustomexception ex) { // here can access properties of exception, add new properties console.writeline(ex.message); } catch(exception otherex) { // other exceptions, useful logging here throw; // better throw otherex since keeps original stacktrace }
here's abstract, simplified example:
public class mycustomexception : exception { public mycustomexception(string msg) : base(msg) { } } public byte[] anymethod() { try { return getbytes(); // exception possible } catch (exception e) { string errormessage = "some custom message, should caller of method should receive"; throw new mycustomexception(errormessage); } }
but note should not use exceptions normal program flow. instead either return true
or false
indicate if action successful or use out
parameter byte[]
int.tryparse
(or other tryparse
methods).
Comments
Post a Comment