Exception handling syntax varies between programming languages to accommodate their overall syntax. Some languages don't call the concept exception handling or they may not have direct facilities for it, but they can still provide means for implementing it.
Haskell does not have special syntax for exception handling, because throwing an exception can be realized generically as a monad. However, the function catch can be used within the IO monad:
do { -- Statements in which errors might be thrown} `catch` ex -> do { -- Statements that execute in the event of an exception, with 'ex' bound to the exception}
In purely functional code, if only one error condition exists, the Maybe monad may be sufficient, and is an instance of Haskell's Monadclass by default. More complex error propagation can be achieved using the Error or ErrorT monads, for which similar syntax (using `catch`) is supported.
The Perl mechanism for exception handling uses die to throw an exception when wrapped inside an eval { ... }; block. After the eval, the special variable $@ contains the value passed from die.
Perl 5.005 added the ability to throw objects as well as strings. This allows for better introspection and handling of types of exceptions.
The __DIE__ pseudo-signal can be trapped to handle calls to die. This is not suitable for exception handling since it is global. However it can be used to convert string-based exceptions from third-party packages into objects.
A number of modules in CPAN expand on the basic mechanism: * Error provides a set of exception classes and allows use of the try/throw/catch/finally syntax. * Exception::Class is a base class and class-maker for derived exception classes. It provides a full structured stack trace in $@->trace and $@->trace->as_string. * Fatal overloads previously defined functions that return true/false e.g. open, close, read, write, etc. This allows built-in functions and others to be used as if they threw exceptions.
PHP
(php5powerprogramming: ISBN 0-13-147149-X, page 77)
PowerBuilder
Exception handling is available in PowerBuilder versions 8.0 and above.
REBOL [ Title: "Exception and error handling examples"]
; TRY a block; capturing an error! and converting to object!if error? exception: try [1 / 0] [probe disarm exception]
; ATTEMPT results in the value of the block or the value none on errorprint attempt [divide 1 0]
; User generated exceptions can be any datatype!example: func ["A function to throw an exception"] [ throw "I'm a string! exception"] catch [example]
; User generated exceptions can also be named,; and functions can include additional run time attributes sophisticated: func ["A function to throw a named error exception" [catch] [ throw/name make error! "I'm an error! exception" 'moniker] catch/name [sophisticated] 'moniker
try { % code that might throw an exception } catch SomeError: { % code that handles this exception } catch SomeOtherError: { % code that handles this exception } finally % optional block { % This code will always get executed }
New exceptions may be created using the new_exception function, e.g., new_exception ("MyIOError", IOError, "My I/O Error");will create an exception called MyIOError as a subclass of IOError.Exceptions may be generated using the throw statement, which can throwarbitrary S-Lang objects.
Visual Basic .NET
Windows PowerShell
ee also
*Exception handling for the semantics of exception handling *Syntax for definition of syntax in computer science
Exception handling — is a programming language construct or computer hardware mechanism designed to handle the occurrence of exceptions, special conditions that change the normal flow of program execution. Programming languages differ considerably in their support… … Wikipedia
Microsoft-specific exception handling mechanisms — Microsoft Windows OS family employs some exception handling mechanisms that are based on the operation system specifics. Contents 1 Structured Exception Handling 1.1 Usage 1.2 Implementation 1.3 … Wikipedia
C Sharp syntax — The correct title of this article is C# syntax. The substitution or omission of the # sign is because of technical restrictions. Main article: C Sharp (programming language) This article describes the syntax of the C# programming language. The… … Wikipedia
JavaScript syntax — This article is part of the JavaScript series. JavaScript JavaScript syntax JavaScript topics This box: view · … Wikipedia
Python syntax and semantics — The syntax of the Python programming language is the set of rules that defines how a Python program will be written and interpreted (by both the runtime system and by human readers). Python was designed to be a highly readable language. It aims… … Wikipedia
PHP syntax and semantics — The syntax of the PHP programming language is the set of rules that defines how a PHP program will be written and interpreted. Overview PHP only parses code within its delimiters. Anything outside its delimiters is sent directly to the output and … Wikipedia
Java-Syntax — Duke, das Java Maskottchen Die Syntax der Programmiersprache Java ist in der Java Language Specification definiert, ebenso wie die Semantik von Java. Dieser Artikel gibt einen Überblick über die Java Syntax und stellt einige ihrer Besonderheiten… … Deutsch Wikipedia
C syntax — The syntax of the C programming language is a set of rules that specifies whether the sequence of characters in a file is conforming C source code. The rules specify how the character sequences are to be chunked into tokens (the lexical grammar) … Wikipedia
ColdFusion — This article is about the computer programming language. For other uses, see Cold Fusion (disambiguation). Adobe ColdFusion Original author(s) Jeremy and JJ Allaire Developer(s) … Wikipedia