multithreading - C# - Thread Abort Exception (Thread Abort Exception) rethrowing itself -
i have current code:
class programme { private static void main() { while (true) { seek { thread.currentthread.abort(); } grab (threadabortexception) { console.writeline("abort!"); thread.resetabort(); } console.writeline("now waiting"); console.readkey(); } } } now know method resetabort supposed prevent threadabortexception go on re-throw when catch statement catching it, question this:
if can utilize resetabort method, what's point of exception specially re-throw itself?
the user can do
grab (threadabortexception ex) { console.writeline("abort!"); throw ex; }
the point of threadabortexception rethrowing create sure thread terminates unless user explicitly calls resetabort.
let me explain:
try { // ... thread abort happens somewhere in here } grab (exception ex) { _log.error(ex); } here have typical illustration of code ensures no exception propagates within try block. know catching exception bad practice, code exists nonetheless.
if phone call abort while thread within try block still want abort. can't rely on users writing sort of code everywhere:
try { // ... thread abort happens somewhere in here } grab (threadabortexception) { throw; // no, you'll never see code in real life } grab (exception ex) { _log.error(ex); } so, in order provide sort of reliable abort, exception has automatically rethrown, or may discarded accident.
resetabort meant rare case when observe thread abort, , know why happenned, , want prevent it.
needless say, utilize cases extremely rare. thread aborts treated runtime in special way, , should avoid them whenever possible. heck, aren't reliable pointed out, , give-and-take ignoring cers create matters worse.
c# multithreading exception
No comments:
Post a Comment