Check out a free preview of the full Professional JS: Features You Need to Know course
The "Error Cause & Hashbang Grammar" Lesson is part of the full, Professional JS: Features You Need to Know course featured in this preview video. Here's what you'd learn in this lesson:
Maximiliano discusses chaining exceptions, where one error is caused by another error. He also explains the hashbang grammar, which is a syntax used in Unix-based systems to specify the interpreter for a script, particularly useful for Node.js scripts.
Transcript from the "Error Cause & Hashbang Grammar" Lesson
[00:00:00]
>> Maximiliano Firtman: Error calls. I'm pretty sure that this came from someone that was a Java developer. So what is that? So when you are using exceptions, you can throw exceptions in JavaScript that has been for a while before ES6 and you can try catch them, okay? You probably all know how to use try catch.
[00:00:22]
But now, every error, when you construct a new error, that's the default constructor for exceptions. An error can include a cause, the cause is another error. So you can cascade errors or exceptions as in Java. There's a pretty common exception design pattern in Java, where you get an exception caused by another exception caused by another exception caused by another exception so on.
[00:00:58]
So, by the way, the APIs in the browser today and the rest of the JavaScript API are not actually making use of that, this is from ES2022. So this is for us, for our exceptions. If we want to include more metadata when you are throwing an exception, including another exception behind scenes.
[00:01:22]
I don't know, for example, you can have a class with a product, whatever. And then you have, for example, a constructor that receives the name and the price. And then you say, okay, this name equals name and we can set the name here as well. Now we can do that, remember this is for ES6, we can do that.
[00:01:51]
And let's say that for the price, I'm going to do something like this. I'm going to say that for some reason I'm going to add 30% of the price, okay? Well, the problem is that what happens if someone is creating a product, I'm creating a P, new product.
[00:02:14]
I'm adding a MacBook Air, and I will say, as a price, too expensive.
>> Maximiliano Firtman: Well, I'm not sure what would happen if I multiply by 130 too expensive. Yeah, it doesn't make any sense, or if I divide, or if I do something like that. So I'm not saying that this case will trigger this.
[00:02:43]
But then, you can say, well, let's try to do this and then catch the exception. And then here, I will throw a new exception from the constructor saying something like, we can't, or our product,
>> Maximiliano Firtman: Can't be created. And then you pass as an argument the cause. And the cause is the other exception that you receive here.
[00:03:12]
Makes sense? So then you're chaining the exceptions, that's the idea. Because before that, maybe let's say if you don't, you can say, use the name, let's say,
>> Maximiliano Firtman: In case it's new or something like that, is a 2, something like that. I'm also going to throw an exception.
[00:03:44]
But in this case the cost is different. So I can create a new error here,
>> Maximiliano Firtman: And pass, and by the way, I'm creating a new exception as a Java. That's wrong, should be a new error. You shouldn't mention JavaScript course because that things happen. You create an error and here you pass another error such as text or Name is invalid.
[00:04:14]
So then, whoever is calling you can listen for your exception. And if the consumer wants, can check the cost. And we have the same exception, the same error, happening with two different courses. Okay, so that's new. It could be emulated before, actually, because you can extend from error, create your own error object.
[00:04:39]
Okay, but now it's part of the error object in the system. Another one that is actually pretty simple and very small if this one, is Hashbang Grammar. Have you ever heard about that? Hashbang Grammar? What is that?
>> Student: It's the praqma for the specific engine to interpret it.
[00:05:04]
>> Maximiliano Firtman: So it's something it's called the shebang, is this syntax. That allows script to be executed or interpreted without you specifying that typically on Unix based systems. Not a Windows, for example, it depends on the terminal you're using. So this is useful on ECMAScript only for now JS scripts, okay?
[00:05:30]
Not for the browser. So if I run this, it's unexpected token. So in the browser, no browser is implementing this part, because it makes no sense. But what's the idea? If you add this at the first line, you're saying I wanna run this script using node, okay? So then you can go to the terminal and just run your script.
[00:05:51]
So if this is a script.js, you go to a terminal and you just type script.js, and you execute it. And in this case, the terminal would check for that line and say, I need to use node, so it will run node over it. Again, It's really simple, now it's part of the language.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops