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

Clarify why exceptions are different. #52

@ghost

Description

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions