Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

Clarify why exceptions are different. #52

Open
ghost opened this issue Sep 26, 2013 · 1 comment
Open

Clarify why exceptions are different. #52

ghost opened this issue Sep 26, 2013 · 1 comment

Comments

@ghost
Copy link

ghost commented Sep 26, 2013

https://github.com/Danack/PHP-to-Javascript#exception-model-is-different and maybe not ? :)
php:

class Foo extends Exception{}
class Foo2 extends Foo{}
class Foo3 extends Foo{}

function testIt($exceptionClass){
    try{
        throw new $exceptionClass();
    }catch(Foo2 $e){
        echo "Foo2".PHP_EOL;
    }catch(Foo $e2){
        echo "Foo".PHP_EOL;
    }
}

testIt('Foo');
testIt('Foo2');
testIt('Foo3');
testIt('Exception');

javascript:

function Exception(message ,code, previous){
    this.message = message || "";
    this.code = code || 0;
    try{a.a=a;}catch(e){this.line=this.file=e;}//parse it for every browser. see https://github.com/eriwen/javascript-stacktrace/blob/master/stacktrace.js#L144
    // implement other php methods
}
function Foo(){}
Foo.prototype=new Exception();
function Foo2(){}
Foo2.prototype=new Foo();
function Foo3(){}
Foo3.prototype=new Foo();

function testIt(exceptionClass){
    try{
        throw new exceptionClass();
    }catch(_e){
        if (_e instanceof Foo2){var e=_e;
            console.log('Foo2');
        }else if (_e instanceof Foo){var e2=_e;
            console.log('Foo');
        }else{
            throw _e;
        }
    }
}

testIt(Foo);
testIt(Foo2);
testIt(Foo3);
testIt(Exception);

output both:

Foo
Foo2
Foo
[object Object] // exception
@Danack
Copy link
Owner

Danack commented Sep 27, 2013

I'll have a look to see if that's possible. There were other issues with how PHP and Javascript generate errors themselves, which apparently I've forgotten to document.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant