<!DOCTYPE html>
<html>
<head>
<script data-require="jquery@*" data-semver="2.2.0" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src=""></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
<h3><a href="http://www.siddharthpandey.net">http://www.siddharthpandey.net</a></h3>
<h3>Learn how to handle exceptions in JavaScript</h3>
<button id="callerButton">Call readTags()</button>
<hr>
<div id="output"></div>
</body>
</html>
// Code goes here
var TagsReader = function() {
var _self = this;
_self.tags = ["Term-1", "Term-2"]
_self.tagsLiteral = "Term-3,Term-4,Term-5,Term-6";
_self.readTags = function() {
var tagsArray = _self.tagsLiteral.split(',');
_self.tags = _self.tags.concat(tagsArray);
}
_self.readTagsWithTryCatch = function() {
try {
var tagsArray = _self.tagsLiteral.split(',');
_self.tags = _self.tags.concat(tagsArray)
} catch (e) {
console.log(e);
// log this exception message somewhere or handle it.
} finally {
// perform any final clean up on success or failure
}
}
_self.readTagsWithDefensiveCoding = function() {
if (_self.tagsLiteral) {
var tagsArray = _self.tagsLiteral.split(',');
_self.tags = _self.tags.concat(tagsArray);
} else {
alert('No tags found!');
}
}
}
$(document).ready(function() {
var reader = new TagsReader();
$('#callerButton').click(function() {
//reader.readTags();
//reader.readTagsWithTryCatch();
reader.readTagsWithDefensiveCoding();
$('#output').html(reader.tags);
});
$('#output').html(reader.tags);
});
/* Styles go here */