I'm pretty new to JavaScript.

I'm trying to define my custom errors, but it doesn't seems to be working

function MyError(msg){
    this.message = msg
    this.name = "My Error"
}

window.onload = function(){
    try{
        throw new MyError("Test Message");
    } catch(e) {
        alert(e.name + ": " + e.message);
    }
}

Getting TypeError, claiming that it's not a function.

To what do you intend "this" to refer to in the body of MyError()? When using "this", it is usually a direct reference to the workable object itself, but unless MyError is a method of a class, it is not in an object context.

Instead, do something like:

function MyError(msg)
	{
	myObj = new Object;
	myObj.message = msg;
	myObj.name = "My Error";
	}

It is also considered good practice to always end every statement with a semicolon.

yeah i figured it out,, it was a stupid typo of myError instead of MyError

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.