Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Cpp Exception Handling

An exception is a problem that arises during the execution of a program. When executing C++ code, different errors can occur: coding errors made by the programmer, errors due to wrong input, or other unforeseeable things such as an attempt to divide by zero.

Exceptions provide a way to transfer control from one part of a program to another.
C++ exception handling is built upon three keywords: try, catch, and throw.

  • The throw keyword throws an exception when a problem is detected, which lets us create a custom error.
  • The try statement allows you to define a block of code to be tested for errors while it is being executed.
  • The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

Assuming a block will raise an exception, a method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception.
Code within a try/catch block is referred to as protected code, and the syntax for using try/catch as follows −

try {
// Block of code to try
throw exception; // Throw an exception when a problem arise
}
catch () {
// Block of code to handle errors
}
// You can list down multiple catch statements to catch different type of exceptions
// in case your try block raises more than one exception in different situations.

Example

try {
string password = "test_pass";
if (password == "test_password") {
cout << "Access granted";
}
else {
throw (password);
}
}
catch (string password) {
cout << "Access denied\n";
cout << "You entered " << password;
}

We use the try block to test some code: If the password variable is not equal to “test_password”, we will throw an exception, and handle it in our catch block.

In the catch block, we catch the error and do something about it. The catch statement takes a parameter: in our example we use a string variable (password) (because we are throwing an exception of string type in the try block (password)), to output the password entered.

If no error occurs, the catch block is skipped.

Note: If you do not know the throw type used in the try block, you can use the “three dots” syntax (...) inside the catch block, which will handle any type of exception.

Try the following example in the editor below.

You are given a function named division which throw an exception if the value of divisor is 0 as shown below. Your task is to complete the main function and use try and catch blocks. In try block call and print the value return by division function, else if an exception is thrown print the exception in catch.

Start solving Cpp Exception Handling on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test