note
	description: "[
		Facilities for adapting the exception handling mechanism.
		This class may be used as ancestor by classes needing its facilities.
	]"
	library: "Free implementation of ELKS library"
	status: "See notice at end of class."
	legal: "See notice at end of class."
	date: "$Date: 2020-05-19 14:32:38 +0000 (Tue, 19 May 2020) $"
	revision: "$Revision: 104260 $"

class 
	EXCEPTIONS

create 
	default_create

feature {NONE} -- Initialization

	default_create
			-- Process instances of classes with no creation clause.
			-- (Default: do nothing.)
			-- (from ANY)
		do
		end
	
feature -- Access

	Check_instruction: INTEGER_32 = 7
			-- Exception code for violated check
			-- (from EXCEP_CONST)

	Class_invariant: INTEGER_32 = 6
			-- Exception code for violated class invariant
			-- (from EXCEP_CONST)

	Com_exception: INTEGER_32 = 28
			-- Exception code for a COM error.
			-- (from EXCEP_CONST)

	Create_on_deferred: INTEGER_32 = 17
			-- Create on deferred
			-- (from EXCEP_CONST)

	Developer_exception: INTEGER_32 = 24
			-- Exception code for developer exception
			-- (from EXCEP_CONST)

	Dollar_applied_to_melted_feature: INTEGER_32 = 26
			-- $ applied to melted feature
			-- (from EXCEP_CONST)

	Eiffel_runtime_fatal_error: INTEGER_32 = 25
			-- Eiffel run-time fatal error
			-- (from EXCEP_CONST)

	Eiffel_runtime_panic: INTEGER_32 = 13
			-- Eiffel run-time panic
			-- (from EXCEP_CONST)

	Exception_in_signal_handler: INTEGER_32 = 20
			-- Exception in signal handler
			-- (from EXCEP_CONST)

	Exception_manager: EXCEPTION_MANAGER
			-- Exception manager
			-- (from EXCEPTION_MANAGER_FACTORY)
		once
			create {ISE_EXCEPTION_MANAGER} Result
		ensure -- from EXCEPTION_MANAGER_FACTORY
			instance_free: class
			exception_manager_not_void: Result /= Void
		end

	External_exception: INTEGER_32 = 18
			-- Exception code for operating system error
			-- which does not set the errno variable
			-- (Unix-specific)
			-- (from EXCEP_CONST)

	Floating_point_exception: INTEGER_32 = 5
			-- Exception code for floating point exception
			-- (from EXCEP_CONST)

	generating_type: TYPE [detachable EXCEPTIONS]
			-- Type of current object
			-- (type of which it is a direct instance)
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			generating_type_not_void: Result /= Void
		end

	generator: STRING_8
			-- Name of current object's generating class
			-- (base class of the type of which it is a direct instance)
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			generator_not_void: Result /= Void
			generator_not_empty: not Result.is_empty
		end

	Incorrect_inspect_value: INTEGER_32 = 9
			-- Exception code for inspect value which is not one
			-- of the inspect constants, if there is no Else_part
			-- (from EXCEP_CONST)

	Io_exception: INTEGER_32 = 21
			-- Exception code for I/O error
			-- (from EXCEP_CONST)

	Loop_invariant: INTEGER_32 = 11
			-- Exception code for violated loop invariant
			-- (from EXCEP_CONST)

	Loop_variant: INTEGER_32 = 10
			-- Exception code for non-decreased loop variant
			-- (from EXCEP_CONST)

	No_more_memory: INTEGER_32 = 2
			-- Exception code for failed memory allocation
			-- (from EXCEP_CONST)

	Number_of_codes: INTEGER_32 = 31
			-- How many codes are there to represent exceptions?
			-- (from EXCEP_CONST)

	Old_exception: INTEGER_32 = 30
			-- Old exception code.
			-- (from EXCEP_CONST)

	Operating_system_exception: INTEGER_32 = 22
			-- Exception code for operating system error
			-- which sets the errno variable
			-- (Unix-specific)
			-- (from EXCEP_CONST)

	Out_of_memory: INTEGER_32 = 15
			-- Out of memory (cannot be ignored)
			-- (from EXCEP_CONST)

	Postcondition: INTEGER_32 = 4
			-- Exception code for violated postcondition
			-- (from EXCEP_CONST)

	Precondition: INTEGER_32 = 3
			-- Exception code for violated precondition
			-- (from EXCEP_CONST)

	Rescue_exception: INTEGER_32 = 14
			-- Exception code for exception in rescue clause
			-- (from EXCEP_CONST)

	Resumption_failed: INTEGER_32 = 16
			-- Resumption failed (retry did not succeed)
			-- (from EXCEP_CONST)

	Retrieve_exception: INTEGER_32 = 23
			-- Exception code for retrieval error
			-- may be raised by retrieved in IO_MEDIUM.
			-- (from EXCEP_CONST)

	Routine_failure: INTEGER_32 = 8
			-- Exception code for failed routine
			-- (from EXCEP_CONST)

	Runtime_check_exception: INTEGER_32 = 29
			-- Exception code for runtime check being violated.
			-- (from EXCEP_CONST)

	Runtime_io_exception: INTEGER_32 = 27
			-- Exception code for I/O error raised by runtime functions
			-- such as store/retrieve, file access...
			-- (from EXCEP_CONST)

	Serialization_exception: INTEGER_32 = 31
			-- Serialization exception code.
			-- (from EXCEP_CONST)

	Signal_exception: INTEGER_32 = 12
			-- Exception code for operating system signal
			-- (from EXCEP_CONST)

	valid_code (c: INTEGER_32): BOOLEAN
			-- Is c a valid code to represent some kind of exceptions?
			-- (from EXCEP_CONST)
		do
			Result := c >= 1 and c <= Number_of_codes
		end

	Void_assigned_to_expanded: INTEGER_32 = 19
			-- Exception code for assignment of void value
			-- to expanded entity
			-- (from EXCEP_CONST)

	Void_call_target: INTEGER_32 = 1
			-- Exception code for feature applied to void reference
			-- (from EXCEP_CONST)
	
feature -- Comparison

	frozen deep_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void
			-- or attached to isomorphic object structures?
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_deep_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			shallow_implies_deep: standard_equal (a, b) implies Result
			both_or_none_void: (a = Void) implies (Result = (b = Void))
			same_type: (Result and (a /= Void)) implies (b /= Void and then a.same_type (b))
			symmetric: Result implies deep_equal (b, a)
		end

	frozen equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached
			-- to objects considered equal?
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.is_equal (b))
		end

	frozen is_deep_equal alias "≡≡≡" (other: EXCEPTIONS): BOOLEAN
			-- Are Current and other attached to isomorphic object structures?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			shallow_implies_deep: standard_is_equal (other) implies Result
			same_type: Result implies same_type (other)
			symmetric: Result implies other.is_deep_equal (Current)
		end

	is_equal (other: EXCEPTIONS): BOOLEAN
			-- Is other attached to an object considered
			-- equal to current object?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			symmetric: Result implies other ~ Current
			consistent: standard_is_equal (other) implies Result
		end

	frozen standard_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached to
			-- field-by-field identical objects of the same type?
			-- Always uses default object comparison criterion.
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.standard_is_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.standard_is_equal (b))
		end

	frozen standard_is_equal alias "" (other: EXCEPTIONS): BOOLEAN
			-- Is other attached to an object of the same type
			-- as current object, and field-by-field identical to it?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			same_type: Result implies same_type (other)
			symmetric: Result implies other.standard_is_equal (Current)
		end
	
feature -- Status report

	assertion_violation: BOOLEAN
			-- Is last exception originally due to a violated
			-- assertion or non-decreasing variant?
		do
			Result := attached Exception_manager.last_exception as l_exception and then attached {ASSERTION_VIOLATION} l_exception.original
		ensure
			instance_free: class
		end

	class_name: detachable STRING_8
			-- Name of the class that includes the recipient
			-- of original form of last exception
		do
			if attached Exception_manager.last_exception as l_exception then
				Result := l_exception.type_name
			end
		ensure
			instance_free: class
		end

	conforms_to (other: ANY): BOOLEAN
			-- Does type of current object conform to type
			-- of other (as per Eiffel: The Language, chapter 13)?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		end

	developer_exception_name: detachable STRING_8
			-- Name of last developer-raised exception
		require
			applicable: is_developer_exception
		do
			if attached Exception_manager.last_exception as e and then attached e.original.description as d then
				Result := {UTF_CONVERTER}.escaped_utf_32_string_to_utf_8_string_8 (d)
			end
		ensure
			instance_free: class
		end

	exception: INTEGER_32
			-- Code of last exception that occurred
		do
			if attached Exception_manager.last_exception as l_exception then
				Result := l_exception.code
			end
		ensure
			instance_free: class
		end

	exception_trace: detachable STRING_8
			-- String representation of the exception trace
		do
			if attached Exception_manager.last_exception as e and then attached e.original.trace as t then
				Result := {UTF_CONVERTER}.escaped_utf_32_string_to_utf_8_string_8 (t)
			end
		ensure
			instance_free: class
		end

	is_developer_exception: BOOLEAN
			-- Is the last exception originally due to
			-- a developer exception?
		do
			Result := attached Exception_manager.last_exception as l_exception and then attached {DEVELOPER_EXCEPTION} l_exception.original
		ensure
			instance_free: class
		end

	is_developer_exception_of_name (name: detachable STRING_8): BOOLEAN
			-- Is the last exception originally due to a developer
			-- exception of name name?
		do
			if is_developer_exception then
				Result := developer_exception_name ~ name
			end
		ensure
			instance_free: class
		end

	is_signal: BOOLEAN
			-- Is last exception originally due to an external
			-- event (operating system signal)?
		do
			Result := attached Exception_manager.last_exception as l_exception and then attached {OPERATING_SYSTEM_SIGNAL_FAILURE} l_exception.original
		ensure
			instance_free: class
		end

	is_system_exception: BOOLEAN
			-- Is last exception originally due to an
			-- external event (operating system error)?
		do
			if attached Exception_manager.last_exception as l_exception and attached Exception_manager.exception_from_code (External_exception) as l_external then
				Result := l_exception.original.conforms_to (l_external) or else attached {OPERATING_SYSTEM_FAILURE} l_exception.original
			end
		ensure
			instance_free: class
		end

	meaning (except: INTEGER_32): detachable STRING_8
			-- A message in English describing what except is
		do
			if attached Exception_manager.exception_from_code (except) as e then
				Result := {UTF_CONVERTER}.string_32_to_utf_8_string_8 (e.Tag)
			end
		ensure
			instance_free: class
		end

	original_class_name: detachable STRING_8
			-- Name of the class that includes the recipient
			-- of original form of last exception
		do
			if attached Exception_manager.last_exception as l_exception then
				Result := l_exception.cause.original.type_name
			end
		ensure
			instance_free: class
		end

	original_exception: INTEGER_32
			-- Original code of last exception that triggered
			-- current exception
		do
			if attached Exception_manager.last_exception as l_exception then
				Result := l_exception.cause.original.code
			end
		ensure
			instance_free: class
		end

	original_recipient_name: detachable STRING_8
			-- Name of the routine whose execution was
			-- interrupted by original form of last exception
		do
			if attached Exception_manager.last_exception as l_exception then
				Result := l_exception.cause.original.recipient_name
			end
		ensure
			instance_free: class
		end

	original_tag_name: detachable STRING_8
			-- Assertion tag for original form of last
			-- assertion violation.
		do
			if attached Exception_manager.last_exception as e and then attached e.cause.original.description as d then
				Result := {UTF_CONVERTER}.escaped_utf_32_string_to_utf_8_string_8 (d)
			end
		ensure
			instance_free: class
		end

	recipient_name: detachable STRING_8
			-- Name of the routine whose execution was
			-- interrupted by last exception
		do
			if attached Exception_manager.last_exception as l_exception then
				Result := l_exception.recipient_name
			end
		ensure
			instance_free: class
		end

	same_type (other: ANY): BOOLEAN
			-- Is type of current object identical to type of other?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			definition: Result = (conforms_to (other) and other.conforms_to (Current))
		end

	tag_name: detachable STRING_8
			-- Tag of last violated assertion clause
		do
			if attached Exception_manager.last_exception as e and then attached e.description as d then
				Result := {UTF_CONVERTER}.escaped_utf_32_string_to_utf_8_string_8 (d)
			end
		ensure
			instance_free: class
		end
	
feature -- Status setting

	catch (code: INTEGER_32)
			-- Make sure that any exception of code code will be
			-- caught. This is the default.
		do
			if attached Exception_manager.type_of_code (code) as l_type then
				Exception_manager.catch (l_type)
			end
		ensure
			instance_free: class
		end

	die (code: INTEGER_32)
			-- Terminate execution with exit status code,
			-- without triggering an exception.
		external
			"C use %"eif_except.h%""
		alias
			"esdie"
		ensure
				False
			instance_free: class
		end

	ignore (code: INTEGER_32)
			-- Make sure that any exception of code code will be
			-- ignored. This is not the default.
		do
			if attached Exception_manager.type_of_code (code) as l_type then
				Exception_manager.ignore (l_type)
			end
		ensure
			instance_free: class
		end

	message_on_failure
			-- Print an exception history table
			-- in case of failure.
			-- This is the default.
		do
			c_trace_exception (True)
		ensure
			instance_free: class
		end

	new_die (code: INTEGER_32)
		obsolete "Use `die'. [2017-05-31]"
		external
			"C use %"eif_except.h%""
		alias
			"esdie"
		ensure
				False
			instance_free: class
		end

	no_message_on_failure
			-- Do not print an exception history table
			-- in case of failure.
		do
			c_trace_exception (False)
		ensure
			instance_free: class
		end

	raise (name: detachable READABLE_STRING_GENERAL)
			-- Raise a developer exception of name name.
		local
			l_exception: DEVELOPER_EXCEPTION
		do
			create l_exception;
			l_exception.set_description (name);
			l_exception.raise
		ensure
			instance_free: class
		end

	raise_retrieval_exception (name: detachable READABLE_STRING_GENERAL)
			-- Raise a retrieval exception of name name.
		do
			if attached Exception_manager.exception_from_code (Serialization_exception) as l_exception then
				l_exception.set_description (name);
				l_exception.raise
			end
		ensure
			instance_free: class
		end
	
feature -- Duplication

	frozen clone (other: detachable ANY): like other
		obsolete "Use `twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- equal to other
			--
			-- For non-void other, clone calls copy;
			-- to change copying/cloning semantics, redefine copy.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.twin
			end
		ensure -- from ANY
			instance_free: class
			equal: Result ~ other
		end

	copy (other: EXCEPTIONS)
			-- Update current object using fields of object attached
			-- to other, so as to yield equal objects.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		external
			"built_in"
		ensure -- from ANY
			is_equal: Current ~ other
		end

	frozen deep_clone (other: detachable ANY): like other
		obsolete "Use `deep_twin' instead. [2017-05-31]"
			-- Void if other is void: otherwise, new object structure
			-- recursively duplicated from the one attached to other
			-- (from ANY)
		do
			if other /= Void then
				Result := other.deep_twin
			end
		ensure -- from ANY
			instance_free: class
			deep_equal: deep_equal (other, Result)
		end

	frozen deep_copy (other: EXCEPTIONS)
			-- Effect equivalent to that of:
			--		copy (other . deep_twin)
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		do
			copy (other.deep_twin)
		ensure -- from ANY
			deep_equal: deep_equal (Current, other)
		end

	frozen deep_twin: EXCEPTIONS
			-- New object structure recursively duplicated from Current.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			deep_twin_not_void: Result /= Void
			deep_equal: deep_equal (Current, Result)
		end

	frozen standard_clone (other: detachable ANY): like other
		obsolete "Use `standard_twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.standard_twin
			end
		ensure -- from ANY
			instance_free: class
			equal: standard_equal (Result, other)
		end

	frozen standard_copy (other: EXCEPTIONS)
			-- Copy every field of other onto corresponding field
			-- of current object.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		external
			"built_in"
		ensure -- from ANY
			is_standard_equal: standard_is_equal (other)
		end

	frozen standard_twin: EXCEPTIONS
			-- New object field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			standard_twin_not_void: Result /= Void
			equal: standard_equal (Result, Current)
		end

	frozen twin: EXCEPTIONS
			-- New object equal to Current
			-- twin calls copy; to change copying/twinning semantics, redefine copy.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			twin_not_void: Result /= Void
			is_equal: Result ~ Current
		end
	
feature -- Basic operations

	frozen as_attached: attached EXCEPTIONS
		obsolete "Remove calls to this feature. [2017-05-31]"
			-- Attached version of Current.
			-- (Can be used during transitional period to convert
			-- non-void-safe classes to void-safe ones.)
			-- (from ANY)
		do
			Result := Current
		end

	frozen default: detachable EXCEPTIONS
			-- Default value of object's type
			-- (from ANY)
		do
		end

	frozen default_pointer: POINTER
			-- Default value of type POINTER
			-- (Avoid the need to write p.default for
			-- some p of type POINTER.)
			-- (from ANY)
		do
		ensure -- from ANY
			instance_free: class
		end

	default_rescue
			-- Process exception for routines with no Rescue clause.
			-- (Default: do nothing.)
			-- (from ANY)
		do
		end

	frozen do_nothing
			-- Execute a null action.
			-- (from ANY)
		do
		ensure -- from ANY
			instance_free: class
		end
	
feature {NONE} -- Implementation

	c_trace_exception (b: BOOLEAN)
		external
			"C use %"eif_except.h%""
		alias
			"eetrace"
		end
	
feature -- Output

	Io: STD_FILES
			-- Handle to standard file setup
			-- (from ANY)
		once
			create Result;
			Result.set_output_default
		ensure -- from ANY
			instance_free: class
			io_not_void: Result /= Void
		end

	out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		do
			Result := tagged_out
		ensure -- from ANY
			out_not_void: Result /= Void
		end

	print (o: detachable ANY)
			-- Write terse external representation of o
			-- on standard output.
			-- (from ANY)
		local
			s: READABLE_STRING_8
		do
			if attached o then
				s := o.out
				if attached {READABLE_STRING_32} s as s32 then
					Io.put_string_32 (s32)
				elseif attached {READABLE_STRING_8} s as s8 then
					Io.put_string (s8)
				else
					Io.put_string_32 (s.as_string_32)
				end
			end
		ensure -- from ANY
			instance_free: class
		end

	frozen tagged_out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			tagged_out_not_void: Result /= Void
		end
	
feature -- Platform

	Operating_environment: OPERATING_ENVIRONMENT
			-- Objects available from the operating system
			-- (from ANY)
		once
			create Result
		ensure -- from ANY
			instance_free: class
			operating_environment_not_void: Result /= Void
		end
	
feature {NONE} -- Retrieval

	frozen internal_correct_mismatch
			-- Called from runtime to perform a proper dynamic dispatch on correct_mismatch
			-- from MISMATCH_CORRECTOR.
			-- (from ANY)
		local
			l_msg: STRING_32
			l_exc: EXCEPTIONS
		do
			if attached {MISMATCH_CORRECTOR} Current as l_corrector then
				l_corrector.correct_mismatch
			else
				create l_msg.make_from_string ("Mismatch: ".as_string_32)
				create l_exc;
				l_msg.append (generating_type.name_32);
				l_exc.raise_retrieval_exception (l_msg)
			end
		end
	
invariant
		-- from ANY
	reflexive_equality: standard_is_equal (Current)
	reflexive_conformance: conforms_to (Current)

note
	copyright: "Copyright (c) 1984-2020, Eiffel Software and others"
	license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
	source: "[
		Eiffel Software
		5949 Hollister Ave., Goleta, CA 93117 USA
		Telephone 805-685-1006, Fax 805-685-6869
		Website http://www.eiffel.com
		Customer support http://support.eiffel.com
	]"

end -- class EXCEPTIONS

Generated by ISE EiffelStudio