0% found this document useful (0 votes)
57 views

Exception Handling (Standard)

The document discusses exception handling in C++. It explains that exceptions occur when errors happen during code execution. C++ uses try, throw, and catch keywords to handle exceptions. The try block contains code that may throw exceptions. The throw keyword throws an exception. The catch block handles any exceptions thrown in the try block.

Uploaded by

Siddharth Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Exception Handling (Standard)

The document discusses exception handling in C++. It explains that exceptions occur when errors happen during code execution. C++ uses try, throw, and catch keywords to handle exceptions. The try block contains code that may throw exceptions. The throw keyword throws an exception. The catch block handles any exceptions thrown in the try block.

Uploaded by

Siddharth Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Exception handling(Standard)

Unit 1
C++ Exceptions

• When executing C++ code, different errors can


occur: coding errors made by the programmer,
errors due to wrong input, or other unforeseeable
things.

• When an error occurs, C++ will normally stop and


generate an error message. The technical term for
this is: C++ will throw an exception (throw an error).
C++ try and catch

• Exception handling in C++ consist of three


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

try {
// Block of code to try
throw exception;
// Throw an exception when a problem arise
}
catch ()
{
// Block of code to handle errors
}
Example

try {
int age = 15;
if (age >= 18)
{
cout << "Access granted - you are old enough.";
}
else {
throw (age);
}
}
catch (int myNum)
{
cout << "Access denied - You must be at least 18 years old.\n";
cout << "Age is: " << myNum;
}
Example explained

• We use the try block to test some code: If


the age variable is less than 18, 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 an int variable (myNum) (because we
are throwing an exception of int type in the try block
(age)), to output the value of age.
• If no error occurs (e.g. if age is 20 instead of 15,
meaning it will be greater than 18), the catch block is
skipped.
• You can also use the throw keyword to output
a reference number, like a custom error
number/code for organizing purposes:
Example
• try {
int age = 15;
if (age >= 18) {
cout << "Access granted - you are old enough.";
} else {
throw 505;
}
}
catch (int myNum) {
cout << "Access denied - You must be at least 18 years old.\n";
cout << "Error number: " << myNum;
}
Handle Any Type of Exceptions (...)

• 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.
Example
• try {
int age = 15;
if (age >= 18) {
cout << "Access granted - you are old enough.";
}
else {
throw 505;
}
}
catch (...) {
cout << "Access denied - You must be at least 18 years old.\n";
}
Question 1

#include <iostream>
using namespace std;
int main()
{
int x = -1;
try {
cout << "Inside try \n";
if (x < 0)
{
throw x;
cout << "After throw \n";
}
}
catch (int x ) {
cout << "Exception Caught \n";
}
cout << "After catch \n";
return 0;
}
• When an exception is thrown, lines of try block after the throw
statement are not executed.

• When exception is caught, the code after catch block is


executed. Catch blocks are generally written at the end
through.
Advantage of Exception Handling
• Remove error-handling code from the
software’s main line of code.
• A method writer can choose to handle certain
exceptions and delegate others to the caller.
• An exception that occurs in a function can be
handled anywhere in the function call stack.
• Separating Error-Handling Code from
“Regular” Code.
What should be put in a try block?

• Statements that might cause exceptions

• Statements that should be skipped in case of


an exception
Question 2

#include<iostream>
using namespace std;

class Base {};


class Derived: public Base {};
int main()
{
Derived d;
try {
throw d;
}
catch(Base b) {
cout<<"Caught Base Exception";
}
catch(Derived d) {
cout<<"Caught Derived Exception";
}
return 0;
• If both base and derived classes are caught as
exceptions then catch block of derived class
must appear before the base class. If we put
base class first then the derived class catch
block will never be reached.

• In Java, catching a base class exception before


derived is not allowed by the compiler itself.

• In C++, compiler might give warning about it,


but compiles the code.
Question 3
#include <iostream>
using namespace std;

int main()
{
try
{
throw 'a';
}
catch (int param)
{
cout << "int exception\n";
}
catch (...)
{
cout << "default exception\n";
}
cout << "After Exception";
return 0;
Output
• default exception
• After Exception

• Explanation: The block catch(…) is used for


catch all, when a data type of a thrown
exception doesn’t match with any other catch
block, the code inside catch(…) is executed.
Question 4
#include <iostream>
using namespace std;
int main()
{
try
{
throw 10;
}
catch (...)
{
cout << "default exception\n";
}
catch (int param)
{
cout << "int exception\n";
}
return 0;
}
• It is compiler error to put catch all block
before any other catch.

• The catch(…) must be the last catch block.


double division(int a, int b)
{
if( b == 0 )
{
throw "Division by zero condition!";
}
return (a/b);
}
#include <iostream>
using namespace std;
double division(int a, int b) {
if( b == 0 ) {
throw "Division by zero condition!";
} return (a/b);
}
int main () {
int x = 50; int y = 0; double z = 0;
try {
z = division(x, y);
cout << z << endl; }
catch (const char* msg) {
cout << msg << endl; }
return 0;
}
• char* is a mutable pointer to
a mutable character/string.

• const char* is a mutable pointer to


an immutable character/string. You cannot change
the contents of the location(s) this pointer points to.
Also, compilers are required to give error messages
when you try to do so. For the same reason,
conversion from const char * to char* is deprecated.

• char* const is an immutable pointer (it cannot point


to any other location) but the contents of location at
which it points are mutable.

You might also like