Skip to main content

SCRUMble ! - New Book about SCRUM

 SCRUMble ! Hello Blog Readers! Thank you for all your support and encouragement. I have something exciting for you all. I have recently written and published a new book called 'SCRUMble !'. It is currently available on pothi store. It will be soon available on Amazon and Flipkart as well. Please get your copy and do let me know your reviews. -Abhishek Sathe SCRUMble ! Written and Published by: Abhishek Sathe                                                                              Distributed by: pothi.com Order your copy now:  https://store.pothi.com/book/abhishek-sathe-scrumble/ Coming soon on Amazon and Flipkart About the book: Scrum is a framework for solving complex problems largely adapted by Software Development field. There are multiple ag...

Exceptions Handling in C++


Exceptions are C++’s means of separating error reporting from error handling.
-Bjarne Stroustrup.


  • Exception Handling is a built in mechanism provided by C++.
  • If a runtime error (i.e. Exception) is not handled properly then the whole program may crash.
  • We use try, catch and throw keywords for exception handling.
  • C++ standard library provides a list of standard exceptions in <exception> header.



try, throw:


Whenever a problem occurs an exception is thrown using throw keyword.

This is done by enclosing the exception causing portion of code inside a try block.

Example:


                 try
                 {

      //statements
      if(condition)
                         throw 2;

                 }

try, throw example:


const int DivideByZero = 10;
//....
double divide(double x, double y)
{
try{
if(y==0)
    throw DivideByZero;
}
return x/y;
}

  • Divide function does division of two numbers

  • If the second number (y) is zero then it throws a DivideByZero exception.

  • If you wish to have your program check for exceptions, you must enclose the code that may have exceptions thrown in a try block.

  • Whenever an exception is thrown, the corresponding try block terminates

catch:



  • catch keyword indicates that the exception that was thrown from the try block is being caught and the code to handle that exception is written inside.

  • Catch block should appear immediately after the try block.

  • For any try block, there could be multiple catch blocks.


catch(char *s)
{
cout<<s;
}


Multiple catch blocks:


Most of the time, a typical program will throw different types of errors. The C++ language allows you to include different catch blocks. Each catch block can face a specific error. The syntax used is:

try {
Code to Try
}
catch(Arg1)
{
One Exception
}
catch(Arg2)
{
Another Exception
}


Standard Exceptions:



The C++ Standard library provides a base class specifically designed to declare objects to be thrown as exceptions. It is called exception and is defined in the <exception> header file under the namespace std.
All exceptions thrown by components of the C++ Standard library throw exceptions derived from this std::exception class. These are:

exceptiondescription
bad_allocthrown by new on allocation failure
bad_exceptionthrown when an exception type doesn't match any catch
bad_typeidthrown by typeid
 

Example:



#include <iostream>
using namespace std;
template <class T>
T max (T &a, T &b)
{
   return (a > b)? a : b;
}
template <>
int max <int> (int &a, int &b)
{
   cout << "Inside specialized template ";
   return (a > b)? a : b;
}

int main ()
{
   int a = 5, b = 10;
   cout << max <int> (a, b);
}

Comments

Popular posts from this blog

Bipartite and semihamiltonian graphs

Bipartite Graph: A graph is bipartite if its vertex set can be partitioned into two subsets X and Y so that every edge has one end in X and one end in Y; such a partition (X,Y) is called bipartition of the graph, and X and Y its parts. ex.- Notation: We denote a bipartite graph G with bipartition (X,Y) by G[X,Y]. Complete Bipartite Graph: If G[X,Y] is simple and every vertex in X is joined to every vertex in Y, then G is called a Complete Bipartite graph. ex.- Semi-Hamiltonian Graph: A semi-Hamiltonian graph is a graph that contains a Hamiltonian path, but not a Hamilton cycle. Hamiltonian Path: A Hamiltonian path in an undirected or directed graph is a path which visits each vertex exactly once. ex.- C-A-D-B-E is a Hamiltonian path Hamiltonian Cycle: A Hamiltonian cycle or a Hamiltonian Circuit is a Hamiltonian path which is a cycle. This post will be useful in understanding a question: "The nabhi kamal grapg is:" (A)Bipartite graph (B)Semi-...

Accessibility : An Important consideration in software development

What is Accessibility? Accessibility is a very important piece of consideration in today's market. It means the quality of being able to be reached or entered. Accessibility focuses on how a physically or mentally disabled person accesses or benefits from a site, system or application.  Conformance with accessibility guidelines while developing a web page or an app is important. What does accessibility mean for software development? Accessibility isn't about taking away functionality or making things difficult for the software development team. It is about making sure all possible users have a way to use the system.  How do information architects and web designers/developers design web pages to be compatible with assistive devices is an important aspect when thinking about accessibility. What does accessibility mean for software testing? Accessibility Testing checks if a product is accessible to the people having disabilities. We need to understand ...

SCRUMble ! - New Book about SCRUM

 SCRUMble ! Hello Blog Readers! Thank you for all your support and encouragement. I have something exciting for you all. I have recently written and published a new book called 'SCRUMble !'. It is currently available on pothi store. It will be soon available on Amazon and Flipkart as well. Please get your copy and do let me know your reviews. -Abhishek Sathe SCRUMble ! Written and Published by: Abhishek Sathe                                                                              Distributed by: pothi.com Order your copy now:  https://store.pothi.com/book/abhishek-sathe-scrumble/ Coming soon on Amazon and Flipkart About the book: Scrum is a framework for solving complex problems largely adapted by Software Development field. There are multiple ag...