Exception handling (programming): Difference between revisions

Content deleted Content added
No edit summary
Citation bot (talk | contribs)
Misc citation tidying. | Use this bot. Report bugs. | Suggested by Abductive | Category:Software anomalies | #UCB_Category 29/34
Line 78:
</syntaxhighlight>
 
C does not have try-catch exception handling, but uses [[return code]]s for error checking. The [[Setjmp.h|<code>setjmp</code> and <code>longjmp</code>]] standard library functions can be used to implement try-catch handling via macros.<ref>{{Cite technicaltech report |url=http://bitsavers.informatik.uni-stuttgart.de/pdf/dec/tech_reports/SRC-RR-40.pdf |title=Implementing Exceptions in C |last=Roberts |first=Eric S. |date=21 March 1989 |publisher=[[DEC Systems Research Center]] |id=SRC-RR-40 |access-date=4 January 2022}}</ref>
 
[[Perl]] 5 uses <code>die</code> for <code>throw</code> and {{code|eval {} if ($@) {}|perl}} for try-catch. It has CPAN modules that offer try-catch semantics.<ref>{{cite book |last1=Christiansen |first1=Tom |last2=Torkington |first2=Nathan |title=Perl cookbook |date=2003 |publisher=O'Reilly |___location=Beijing |isbn=0-596-00313-7 |edition=2nd |url=https://docstore.mik.ua/orelly/perl4/cook/ch10_13.htm |chapter=10.12. Handling Exceptions}}</ref>
Line 196:
 
== Asynchronous exceptions ==
'''Asynchronous exceptions''' are events raised by a separate thread or external process, such as pressing [[Control-C|Ctrl-C]] to interrupt a program, receiving a [[Signal (computing)|signal]], or sending a disruptive message such as "stop" or "suspend" from another [[Thread (computer science)|thread of execution]].<ref>{{cite web |url=http://citeseer.ist.psu.edu/415348.html |title=Asynchronous Exceptions in Haskell - Marlow, Jones, Moran (ResearchIndex) |publisher=Citeseer.ist.psu.edu |access-date=2011-12-15 |url-status=live |archive-url=http://archive.wikiwix.com/cache/20110223164151/http://citeseer.ist.psu.edu/415348.html |archive-date=2011-02-23 }}</ref><ref>{{cite technicaltech report|url=http://www.cs.williams.edu/~freund/papers/python.pdf |title=Safe Asynchronous Exceptions For Python |access-date=4 January 2022 |first1=Stephen N.|last1=Freund|first2=Mark P.|last2=Mitchell }}</ref> Whereas synchronous exceptions happen at a specific <code>throw</code> statement, asynchronous exceptions can be raised at any time. It follows that asynchronous exception handling can't be optimized out by the compiler, as it cannot prove the absence of asynchronous exceptions. They are also difficult to program with correctly, as asynchronous exceptions must be blocked during cleanup operations to avoid resource leaks.
 
Programming languages typically avoid or restrict asynchronous exception handling, for example C++ forbids raising exceptions from signal handlers, and Java has deprecated the use of its ThreadDeath exception that was used to allow one thread to stop another one.<ref>{{cite web |url=http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html |title=Java Thread Primitive Deprecation |publisher=Java.sun.com |access-date=2011-12-15 |url-status=live |archive-url=https://web.archive.org/web/20090426200153/http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html |archive-date=2009-04-26 }}</ref> Another feature is a semi-asynchronous mechanism that raises an asynchronous exception only during certain operations of the program. For example, Java's {{C++|Thread.interrupt()}} only affects the thread when the thread calls an operation that throws {{C++|InterruptedException}}.<ref>{{cite web |title=Interrupts (The Java™ Tutorials > Essential Java Classes > Concurrency) |url=https://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html |website=docs.oracle.com |access-date=5 January 2022}}</ref> The similar POSIX {{C++|pthread_cancel}} API has race conditions which make it impossible to use safely.<ref>{{cite web |last1=Felker |first1=Rich |title=Thread cancellation and resource leaks |url=http://ewontfix.com/2/ |website=ewontfix.com|access-date=5 January 2022}}</ref>