Sponsored Links

Rabu, 29 November 2017

Sponsored Links

C# Beginner to advanced - Lesson 34 - Exception Handling (try ...
src: i.ytimg.com

Exception handling syntax varies between programming languages, partly to cover semantic differences but largely to fit into each language's overall syntactic structure. Some languages do not call the relevant concept 'exception handling'; others may not have direct facilities for it, but can still provide means to implement it.

Most commonly, error handling uses a try...[catch...][finally...] block, and errors are created via a throw statement, but there is significant variation in naming and syntax.


Video Exception handling syntax



Catalogue of exception handling syntaxes

Ada

Exception declarations
Raising exceptions
Exception handling and propagation

Assembly language

Most assembly languages will have a macro instruction or an interrupt address available for the particular system to intercept events such as illegal op codes, program check, data errors, overflow, divide by zero, and other such. IBM and Univac mainframes had the STXIT macro. Digital Equipment Corporation RT11 systems had trap vectors for program errors, i/o interrupts, and such. DOS has certain interrupt addresses. Microsoft Windows has specific module calls to trap program errors.

Bash

One can set a trap for multiple errors, responding to any signal with syntax like:

trap 'echo Error at line ${LINENO}' ERR

BASIC

An On Error goto/gosub structure is used in BASIC and is quite different from modern exception handling; in BASIC there is only one global handler whereas in modern exception handling, exception handlers are stacked.

C

The most common way to implement exception handling in standard C is to use setjmp/longjmp functions:

Microsoft-specific

Two types exist:

  • Structured Exception Handling (SEH)
  • Vectored Exception Handling (VEH, introduced in Windows XP)

Example of SEH in C programming language:

C#

C++

In C++, a resource acquisition is initialization technique can be used to clean up resources in exceptional situations. C++ intentionally does not support finally. The outer braces for the method are optional.

ColdFusion Markup Language (CFML)

Script syntax

Adobe ColdFusion documentation

Tag syntax

Adobe ColdFusion documentation

Railo-Lucee specific syntax

Added to the standard syntax above, CFML dialects of Railo and Lucee allow a retry statement.

This statement returns processing to the start of the prior try block.

CFScript example:

Tag-syntax example:

D

In D, a finally clause or the resource acquisition is initialization technique can be used to clean up resources in exceptional situations.

Delphi

Exception declarations
Raising exceptions
Exception handling and propagation

Erlang

Haskell

Haskell does not have special syntax for exceptions. Instead, a try/catch/finally/etc. interface is provided by functions.

prints

  (1,42)  

in analogy with this C++

Another example is

In purely functional code, if only one error condition exists, the Maybe type may be sufficient, and is an instance of Haskell's Monad class by default. More complex error propagation can be achieved using the Error or ErrorT monads, for which similar functionality (using `catch`) is supported.

Java

JavaScript

Lisp

Common Lisp

Lua

Lua uses the pcall and xpcall functions, with xpcall taking a function to act as a catch block.

Predefined function
Anonymous function

Objective-C

Exception declarations
Raising exceptions
Exception handling and propagation

OCaml

Perl

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. However, scoping issues can make doing this correctly quite ugly:

Perl 5.005 added the ability to throw objects as well as strings. This allows 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.

The forms shown above can sometimes fail if the global variable $@ is changed between when the exception is thrown and when it is checked in the if ($@) statement. This can happen in multi-threaded environments, or even in single-threaded environments when other code (typically called in the destruction of some object) resets the global variable before the checking code. The following example shows a way to avoid this problem (see [1]). But at the cost of not being able to use return values:

Several modules in the Comprehensive Perl Archive Network (CPAN) expand on the basic mechanism:

  • Error provides a set of exception classes and allows use of the try/throw/catch/finally syntax.
  • TryCatch and Try::Tiny both allow use of try/catch/finally syntax instead of boilerplate to handle exceptions correctly.
  • 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

PowerBuilder

Exception handling is available in PowerBuilder versions 8.0 and above.

  TRY     // Normal execution path  CATCH (ExampleException ee)     //  deal with the ExampleException  FINALLY     // This optional section is executed upon termination of any of the try or catch blocks above  END TRY  

PowerShell

Version 1.0

Version 2.0

Python

R

Rebol

Rexx

Ruby

S-Lang

   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 throw arbitrary S-Lang objects.

Smalltalk

The general mechanism is provided by the message on:do:. Exceptions are just normal objects that subclass Error, you throw one by creating an instance and sending it a #signal message, e.g., MyException new signal. The handling mechanism (#on:do:) is again just a normal message implemented by BlockClosure. The thrown exception is passed as a parameter to the handling block closure, and can be queried, as well as potentially sending #resume to it, to allow execution flow to continue.

Swift

Exception handling is supported since Swift 2.

Tcl

Since Tcl 8.6, there is also a try command:

VBScript

Visual Basic

Visual Basic .NET

Visual Prolog

http://wiki.visual-prolog.com/index.php?title=Language_Reference/Terms#Try-catch-finally

X++


Maps Exception handling syntax



References


Chapter 9: Error handling with exceptions ○ Syntax errors can be ...
src: images.slideplayer.com


See also

  • Exception handling for the semantics of exception handling
  • Syntax for definition of syntax in computer science

Source of the article : Wikipedia

Comments
0 Comments