Thursday, November 11, 2021
JavaScript error handling is a lot like other languages where you have the try/catch/finally statement that you can used. The basic premise is
try
{
// try some codes here fingers crossed that nothing bad happens
}
catch(ex)
{
// oops something bad happened, this is where it gets handled or I tell the user
}
finally
{
// I don't care what happens I am going to execute and get paid
}
Here is the error handling in action:
Let's say type in the following code with the eval( ) function that you know will cause a "Syntax Error". The catch statement we are going to display the exception in the console. In finally statement we do another evaluation which will execute regardless of whether the try statement was successful.
try
{
var myvar = "my variable";
eval(myvar)
}
catch(ex)
{
console.log("This is the exception " + ex);
}
finally
{
console.log(eval(1 + 2));
}
Here is the output for the code above:
try
{
// try some codes here fingers crossed that nothing bad happens
}
catch(ex)
{
// oops something bad happened, this is where it gets handled or I tell the user
}
finally
{
// I don't care what happens I am going to execute and get paid
}
Here is the error handling in action:
Let's say type in the following code with the eval( ) function that you know will cause a "Syntax Error". The catch statement we are going to display the exception in the console. In finally statement we do another evaluation which will execute regardless of whether the try statement was successful.
try
{
var myvar = "my variable";
eval(myvar)
}
catch(ex)
{
console.log("This is the exception " + ex);
}
finally
{
console.log(eval(1 + 2));
}
Here is the output for the code above:
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
No comments:
Post a Comment