Created
March 28, 2012 14:41
-
-
Save aminin/2226726 to your computer and use it in GitHub Desktop.
Стек вызовов при пробрасывании исключений
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Этот пример демонстрирует понятие "цепочки исключений" | |
| // Показывает сохранение стэка вызовов (stack trace) при оборачивании исключений | |
| function a() | |
| { | |
| try { | |
| b(); | |
| } catch (BException $e) { | |
| throw new AException('Bar', 1, $e); | |
| } | |
| } | |
| function b() | |
| { | |
| throw new BException('Foo'); | |
| } | |
| function c() | |
| { | |
| try { | |
| a(); | |
| } catch (AException $e) { | |
| throw new CException('Baz', 1, $e); | |
| } | |
| } | |
| class AException extends Exception {} | |
| class BException extends Exception {} | |
| class CException extends Exception {} | |
| c(); | |
| // Показывает сохранение стэка вызовов (stack trace) при пробрасывании | |
| function a1() | |
| { | |
| try { | |
| b1(); | |
| } catch (Exception $e) { | |
| throw $e; | |
| } | |
| } | |
| function b1() | |
| { | |
| throw new BException('Foo1'); | |
| } | |
| function c1() | |
| { | |
| try { | |
| a1(); | |
| } catch (Exception $e) { | |
| throw $e; | |
| } | |
| } | |
| c1(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ~*~ encoding: utf-8 ~*~ | |
| # Этот пример демонстрирует понятие "цепочки исключений" | |
| # Показывает сохранение стэка вызовов (stack trace) при оборачивании исключений | |
| class CausalException < Exception | |
| attr_accessor :cause | |
| def initialize(msg = nil, cause = nil) | |
| super msg | |
| self.cause = cause | |
| end | |
| def cause= cause | |
| @cause = cause if cause.is_a? Exception | |
| end | |
| def primary_cause | |
| pc = self | |
| pc = pc.cause while pc.respond_to?(:cause) && pc.cause.is_a?(Exception) | |
| pc | |
| end | |
| end | |
| class AException < CausalException | |
| end | |
| class BException < CausalException | |
| end | |
| class CException < CausalException | |
| end | |
| def a() | |
| begin | |
| b(); | |
| rescue BException => e | |
| raise AException.new('Bar', e); | |
| end | |
| end | |
| def b() | |
| raise BException.new('Foo'); | |
| end | |
| def c() | |
| begin | |
| a(); | |
| rescue AException => e | |
| raise CException.new('Baz', e); | |
| end | |
| end | |
| begin | |
| c(); | |
| rescue CausalException => e | |
| puts e.primary_cause.backtrace.join("\n") | |
| end | |
| exit | |
| # Показывает сохранение стэка вызовов (stack trace) при пробрасывании | |
| def a1() | |
| begin | |
| b1(); | |
| rescue Exception => e | |
| raise e; | |
| end | |
| end | |
| def b1() | |
| raise BException.new('Foo1'); | |
| end | |
| def c1() | |
| begin | |
| a1(); | |
| rescue Exception => e | |
| raise e | |
| end | |
| end | |
| c1(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment