try{
let result = someFunction();
console.log(result);
} catch(error){
console.log(error.message);
}finally{
console.log("code executed");
}
try{
let age = 15;
if(age > 18){
throw new Error("you must be at least 18 years old");
}
console.log("access granted");
} catch(error){
console.log(error.message);
}
// program with error:
try{
let message = "hello world";
console.log(messages);
}catch(error){
console.log(error.message);
}
finally{
console.log("final block executed");
}
// program without an error:
try{
let num = 45;
console.log(num);
}catch(error){
console.log(error.message);
}
finally{
console.log("final block executed");
}
function division(num1,num2){
try{
if(num2 === 0){
throw new Error("Cannot divide by zero");
}
let result = num1 / num2;
console.log(result);
} catch(error){
console.log(error.message);
}finally{
console.log("finally block executed")
}
}
division(50,0)
OOP:
Object oriented programming(OOP) is a programming paradigm that focuses on
organizing code into objects that encapsulates data and behaviours. It allows you to
create reusable and modular code by representing real world entities or abstract
concepts as objects.
1) class and objects
2) inheritance
3) polymorphism
4) encapsulation
5) abstraction.
1) class:
a class is a blueprint or template that defines the structure and behaviour of
objects. it encapsulates data and related methods into single entity.
classes can be defined using the class keyword(introduced in ES6).
2) objects:
an object is an instance of a class. it represents a realworld entity or a concept
and encapsulates data and related behaviours. objects are created from classes or
constructor function. they have their own unique state(property values) and can
perform actions(methods) defined within the class.
class rectangle{
constructor(width, height){
this.width = width;
this.height = height;
}
area(){
return this.width * this.height;
}
}
let result = new rectangle(15,50);
console.log(result.area())
Sequencial Structure:-
It represents a straight forward linear flow of code execution while function, constructor, classes and objects provide ways to organize and structure code for better modularity, reusability, and abstraction.
Inheritance:-
It is a mechanism that allows a class to inherit properties and method from another class, establishing a heirarchical relationship between classes. It provides code reusability and facilitates creating specialized classes based on exciting ones.
Exersise

0 Comments