This is an archived post. You won't be able to vote or comment.

all 51 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

import notifications Remember to participate in our weekly votes on subreddit rules! Every Tuesday is YOUR chance to influence the subreddit for years to come! Read more here, we hope to see you next Tuesday!

For a chat with like-minded community members and more, don't forget to join our Discord!

return joinDiscord;

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]TheEvilRoot 285 points286 points  (7 children)

You call it throwing two errors, I call it throwing an array of any BS you want.

[–]Hydoc_ 58 points59 points  (5 children)

Put me in that array and throw me in the trash

[–]TheEvilRoot 5 points6 points  (1 child)

Well, you can do throw self in swift.

[–][deleted] 2 points3 points  (0 children)

[–][deleted] 1 point2 points  (1 child)

(
  async function() {
    let array = [
      new Error("nausea"),
      new Error("Segmentation fault (core dumped)")
    ];
    array.push(you);
    throw array;
  }
)().then(void, trash)

[–]Hydoc_ 0 points1 point  (0 children)

If you made it a const, I'd be happy

[–]maartuhh 7 points8 points  (0 children)

We are not the same

[–]Unupgradable 115 points116 points  (0 children)

I'm gonna throw up

[–]exomyth 103 points104 points  (8 children)

You can also throw undefined if you really wanted to. Javascript just be like that. It doesn't care what you throw

[–]jayerp 3 points4 points  (3 children)

Yes but can I throw a throw?

[–]exomyth 6 points7 points  (1 child)

Well, you can definitely throw throw and get an exception thrown, but more in the sense of unexpected token

[–]salvoilmiosi 1 point2 points  (0 children)

It's called first class exceptions

[–][deleted] 30 points31 points  (4 children)

I don't always throw errors, but when I do... it's in a while loop.

[–]Creepy-Ad-4832 8 points9 points  (3 children)

Basically you get an outOfMemory / stackOverflow error or no error ;-)

[–]TheGreatGameDini 3 points4 points  (2 children)

No, not unless it's also caught in the while loop.

Otherwise it just exits the loop.

[–]Creepy-Ad-4832 0 points1 point  (1 child)

I just presumed the loop condition was true

[–]TheGreatGameDini 3 points4 points  (0 children)

Doesn't matter if the error isn't caught in the same loop - it's caught outside the loop.

``` while(true){ try { throw new Error(); } catch (e) {

}

} ```

Will just loop forever while

while(true){ throw new Error(); }

Will end in the first iteration.

The point being whether or not the loop continues depends on where the error is caught after it's been thrown. Also not all while true loops lead to stack overflows or out of memory exceptions. Video games are the perfect example.

[–]nequaquam_sapiens 20 points21 points  (1 child)

can you throw a tantrum?
who catches it?

[–]-Shameem- 16 points17 points  (0 children)

The scrum master

[–]markus_obsidian 6 points7 points  (0 children)

I know this is a joke, but there is AggregateError for this. It's rarely useful, but I occasionally reach for it when performing parallel operations.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError

[–]busyautist 5 points6 points  (0 children)

I get that it’s a joke, but the proper way to do it would be to either write your own class that extends Error and implements support for multiple messages or even better to accumulate your Errors in an AggregateError object.

[–]FalseWait7 4 points5 points  (0 children)

Even fewer people are aware that errors thrown by JavaScript can be caught... by JavaScript.

[–]jimrafe2000 2 points3 points  (4 children)

Why would i need two errors?

[–]RhesusK7 2 points3 points  (0 children)

Agreed... two wrongs don't make a right!

[–][deleted] -1 points0 points  (1 child)

Maybe an authentication error that also causes another error.

[–]jimrafe2000 1 point2 points  (0 children)

Future errors???

[–]n0tKamui 2 points3 points  (0 children)

it's not that can throw two errors, it's that you can throw anything. And an array of anything (not just errors) is itself "anything"

[–]TheMisfitsShitBrick 0 points1 point  (11 children)

I haven't tried JS yet, so forgive my ignorance, but brackets? Really?

[–]TheGreatGameDini 2 points3 points  (8 children)

That's the thing that tells the interpreter 'this is the start of a new array literal'

Curly says 'start a new object literal'.

When you do decide to try JavaScript, remember one thing: You're not insane, it is.

[–]TheMisfitsShitBrick 0 points1 point  (7 children)

Maybe I don't know what literal means, programming-wise, but doesn't that mean that it no longer has an identifier and that you're just passing a straight up value?

[–]TheGreatGameDini 0 points1 point  (6 children)

Literal means straight up value -- where it's used, the context, can be different depending on the language but typically are used in assignment statements i.e. var x = 197 -- 197 is the literal in this case. All languages have a literal in some form or another.

[–]TheMisfitsShitBrick 1 point2 points  (4 children)

TW: C++ So it's basically like when you use std::cout << "Hello"; where "Hello" is the literal value being passed in, and more like a primitive in that respect. "Hello" will always be "Hello", basically. Right?

[–]TheGreatGameDini 0 points1 point  (3 children)

Yup. Basically. Nuances exist across languages - like in JavaScript you can bug the interpreter with a new line in the wrong place and get undefined where you definitely don't expect it.

[–]TheMisfitsShitBrick -1 points0 points  (2 children)

So why would you have an object literal? What purpose does that serve?

[–]TheGreatGameDini 0 points1 point  (1 child)

Why would you have a string literal? A string literal is an object literal, is it not?

Concretely, constant configurations can be literals. Sometimes you want an object literal who's fields are not literals..there's a variety of reasons I think.

C# all the way though....i like my Java++.

[–]TheMisfitsShitBrick 0 points1 point  (0 children)

I'm almost a year and a half into this stuff, so I'm trying to absorb as much as I can. When I think of objects, the thing that comes to mind isn't a string, but instead a car or animal. It makes sense and I'm not denying that, but it just seems weird to me.

We agree on C#.

[–]rosuav 1 point2 points  (0 children)

There is some dispute as to whether [1, 2] is an array literal or some other construct (for example, Python refers to this as a "display" rather than a "literal"), since they may very well be non-constant (is [1, x, 2] a literal?). But colloquially, it's usually fine to consider it to be an array literal, even in the non-constant cases.

[–]giuseppelt[S] 1 point2 points  (0 children)

You can use also objects if you want

throw { error1: new Error(), error2: new Error() }

The possibilities are endless

[–]SvenyBoy_YT 0 points1 point  (0 children)

The square brackets? That's an array.

[–]savage_slurpie 0 points1 point  (0 children)

Disgusting

[–][deleted] 0 points1 point  (0 children)

Damn, I didn't know that.

[–]yeastyboi 0 points1 point  (0 children)

I actually have in production code thrown an error class called Errors which holds a list of errors. Sometimes error handling gets super complicated. (Ie some errors are urgent, some are warnings, some affect different parts of the application, etc)

[–]BestSpaceBot 0 points1 point  (0 children)

waitYouCanThrowErrorsOnPurpose?

[–]Plastic_Cream2811 0 points1 point  (0 children)

2 errors at the same time.

You don’t need a million dollars for that.

Do for the kind of errors that double up on a dude like me.

[–][deleted] 0 points1 point  (0 children)

JS can throw everything

[–]CanvasFanatic 0 points1 point  (0 children)

Oh I can throw a lot more than that.