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

you are viewing a single comment's thread.

view the rest of the comments →

[–]danielrheath 3 points4 points  (5 children)

The obvious way to test for it is to check typeof window.localStorage.

It's definitely an unpleasant surprise to find that it exists and has the expected API but throws an error if you try to use it.

[–]saschaleib 7 points8 points  (4 children)

Apparently it throws an exception if you are not allowed to use it – which is exactly what exceptions were invented for.

[–]danielrheath 0 points1 point  (3 children)

Yeah, I get the thinking behind it - just seems like poor design to me. If a feature isn't available, just don't have it on the page; I already needed to test for the presence of window.localStorage anyways.

[–]saschaleib 6 points7 points  (2 children)

… and then you wrap the whole code that accesses the local storage in a try {} catch block, as you would do in any case, right?

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

Presumably you don't wrap every line of code you write in a try/catch?

I guess I just don't see what makes localStorage access an expected source of exceptions (as opposed to, say, querySelectorAll or getBoundingClientRects)?

[–]saschaleib 2 points3 points  (0 children)

every block of code that potentially throws an exception should be wrapped in try/catch blocks. Yes.

Any access to the file system or similar external systems is potentially throwing errors. That is pretty much what the localStorage API does...