.net - Custom exception- Try catch - in c# -


it allowed use custom exception, exception can thrown below.

  try {     int foo = int.parse(token); } catch (formatexception ex) {     //assuming added constructor     throw new parserexception(       $"failed read {token} number.",        filename,        linenumber,        ex); } 

but in normal try catch block, says , throwing exceptions clear stacktrace.

 try       {         forthcall();       }       catch (exception ex)       {         throw ex;       } 

so in custom exception,how managed use throw exception, without clear stacktrace?

there several ways can done.

as mentioned in link in c#, how can rethrow innerexception without losing stack trace?, can use exceptiondispatchinfo class code similar

try {     task.wait(); } catch(aggregateexception ex) {     exceptiondispatchinfo.capture(ex.innerexception).throw(); } 

another way have handler return boolean, whether exception handled or not, can use in catch clause:

catch (exception ex) {   if (!handleexception(ex)) {     throw;   } } 

where handleexception custom exception handler. gotten link: how throw exception without resetting stack trace?


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -