What is an exception

What is an exception? | c++ Advanced | Part – 10

What do you do when you see a speed breaker in front of you?

You apply brakes, right?

The speed breaker breaks down your normal speed.

This is exactly what exception does to our program.

Exception is an event that disrupts the normal flow of the program.

Exceptions are the errors which occur during the execution of the program.

Exception handling enables us to handle runtime errors.

Thus, exception handling helps us maintain the normal flow of the program.

Exceptions can occur due to user’s error, programmer’s error or due to the failure of any physical resource.

Try, throw and catch

Try, catch and throw are used to handle exceptions in C++.

The try block is used to enclose the block of code which might throw an exception.

The try block is always followed by one or more catch blocks.

The catch block is used to handle the exception.

It is always used after a try block.

There can be multiple catch blocks followed by a try block.

try{

//code

} catch(exception_name e){

//catch block

}

This is how a try catch block looks like.

exception_name e points to the exception thrown if any.

Let’s write a try, catch block in order to catch the exception thrown when a number is divided by 0.

 int main() {

try{

int num = 40/0; //Division by 0

}

catch(char *e){

cout<<e;

}

}

The output of the above snippet will be

warning: division by zero[-Wdiv-by-zero] int num = 40/0;

Division by zero is the exception which has occurred.

The warning message displays the exception caught in the catch block.

This is how the try catch block works.

Let’s see why throw is used.

Throw

The throw keyword is used to explicitly throw an exception.

The syntax of the throw exception is thrown exception;

Let’s see how it works.

void divide(int num){

if(num == 0){

throw “Division by 0 Exception“;

}

}

int main() {

divide(0);

}

The output of the above snippet will be terminate called after throwing an instance of ‘char const*’

Thus, using throw we have thrown an exception manually.

The pieces of code after throw if any will never be executed.

Awesome, we have learnt all about exception handling with chickens.

To summarize

Exception is an event that disrupts the normal flow of the program.

Exception handling maintains the normal flow of the program.

The try block is used to enclose the block of code which might throw an exception.

The catch block is used to handle the exception.

The throw keyword is used to directly throw an exception.

Leave a Comment

Your email address will not be published. Required fields are marked *

PYQ of History UGC NET UGC NET Mathematical Reasoning and Aptitude ICT (Digital Initiatives) | UGC NET | paper – 1 The Scope of Information and Communication Technology(ICT) PYQ of People, Development, and Environment Top 30 PYQ of HINDI | UGC NET – 2023 Top 30 PYQ of Teaching Aptitude PYQ of Research Aptitude | NTA UGC NET | Paper 1 | Part 1 UGC NET Paper 1 Syllabus | Updated | 2023 Types of Research | Research Aptitude | nta ugc net | UGC NET 2023