Showing posts with label Exceptions. Show all posts
Showing posts with label Exceptions. Show all posts

Wednesday, 27 October 2010

'new operator' throw and nothrow

I used to always think and while coding take care that to check if memory allocation is successful, we need to check if the returned pointer is not NULL. Well, in MSVC8 this may not necessarily be true. Depending on the library you include (see here) you may end up with either a throw or a NULL. The partial good news is that you can in future write your code to not throw memory alloc failure. See example:



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include<iostream>

using namespace
std;

int
main()
{

try

{

char
* p = new char[0x7fffffff];
if
(!p)
{

cout<<"Memory allocation for q failed. NULL returned."<<endl;
}
}

catch
(...)
{

cout<<"Exception caught by ..."<<endl;
}


char
* q = new (std::nothrow) char[0x7fffffff];
if
(!q)
{

cout<<"Memory allocation for q failed. NULL returned."<<endl;
}


return
0;
}




The output is as follows:

Wednesday, 12 May 2010

Interesting Challenging problem on Code Complexity

Picked up this interesting problem from the following book:



How many execution paths can this simple three line code take:



String EvaluateSalaryAndReturnName( Employee e )
{

if
( e.Title() == "CEO" || e.Salary() > 100000 )
{

cout << e.First() << " " << e.Last() << " is overpaid" << endl;
}

return
e.First() + " " + e.Last();
}






The answer may surprise you. I have embedded the actual pages from Google books. Luckily the whole problem and solution is given. See Item 18.

Wednesday, 9 December 2009

Catching the 'Divide By Zero' exceptions

I was surprised to find that C++ does not catch divide by 0 exceptions by default. The code will crash when a divide by 0 situation occurs. As a result the only option is to write our own class or method to handle divide by 0 scenario. The example below shows how to handle the Divide by zero scenario and how to construct an exception.







//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include<iostream>

using namespace
std;

class
Division
{

public
:
double
quotient(int numerator, int denominator);
private
:

};


double
Division::quotient(int numerator, int denominator)
{

if
(!denominator)
{

exception e("Divide by 0 exception");
throw
(e);
}

return
(static_cast< double >( numerator ))/denominator;
}



int
main()
{

Division d;
try

{

cout<<"1. (100/10) = "<<d.quotient(100, 10)<<endl;
cout<<"2. (10/4) = "<<d.quotient(10, 4)<<endl;
cout<<"3. (10/0) = "<<d.quotient(10, 0)<<endl;
cout<<"4. (30/9) = "<<d.quotient(30, 9)<<endl;
}

catch
(exception& e)
{

cout<<"Exception caught with cause :"<<e.what()<<endl;
}

return
0;
}








The output is as follows:


You can read more on this topic here.

Thursday, 30 April 2009

Abuse of try and catch in exception handling

I have seen in some real life code that people sometimes use C++ for their convinience. Rather than solving the problem using powerful C++ features, they take dodgy shortcuts. Here is an example of peice of code that shows this abuse with 2 possible solutions.


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This example shows how some people abuse throw and catch

#include<iostream>
using namespace std;
//
//The following interface is defined and being used by many classes
void function1();
//Now some programmer wants to change this to
//int function1();
//But this is not possible since overloading does not work for return types
//
//The same problem could instead be solved by other approach as follows:
void function1(int &abc);
int
function1(int dummy1, int dummy2); /*Note 2 dummy to avoid ambiguity with
the first overload. If the above overloaded func was not
there then only 1 dummy is sufficient
*/


int
main()
{

try

{

function1();
}

catch
(int xyz)
{

cout<<"\n\n";
cout<<"The function1() returned "<<xyz<<endl;
cout<<"\n\n";
}


int
first;
function1(first);
cout<<"The 1st overloaded function1() returned "<<first<<endl;
cout<<"\n\n";

cout<<"The 2nd overloaded function1() returned "<<function1(3,4)<<endl;
cout<<"\n\n";

return
0;
}


void
function1()
{

//function logic
throw 100;
}


//Overloaded function1()
void function1(int &abc)
{

abc = 120;
}


//Overloaded function1()
int function1(int dummy1, int dummy2)
{

return
140;
}





The output is as follows:

Wednesday, 15 April 2009

A simple example of C++ Exceptions

Exceptions is a very powerful mechanism by which errors can be handled within the programs in C++. In C if an exception occurs (for example overflow, underflow, memory allocation failure, etc) then the program would just crash. In C++ these problems can be handled by use of exceptions.


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This example shows different ways of throwing and catching
//exceptions and handling of exceptions

#include<iostream>
//#include<exception> //May be required on other platforms

using namespace
std;

void
function1();
void
function2();
void
function3();
void
function4();

class
zgException: public exception
{

//overloading the what() method
virtual const char* what() const throw()
{

return
"zgException occured";
}
}
zgEx;


int
main()
{

function1();
function2();
function3();
function4();

return
0;
}


//Throwing an integer
void function1()
{

cout<<"\nfunction1()"<<endl;
try

{

throw
123;
}

catch
(int a)
{

cout<<"Exception caught with value "<<a<<endl;
}
}


//Throwing a string but no mechanism to catch it
void function2()
{

cout<<"\nfunction2()"<<endl;
string s = "function2() exception";
try

{

throw
s;
}

catch
(exception &e)
{

cout<<"Exception message: "<<e.what()<<endl;
}

catch
(...)
{

cout<<"All exceptions not handled are caught here"<<endl;
}
}


//Throwing a string and catching it
void function3()
{

cout<<"\nfunction3()"<<endl;
string s = "function3() exception";
try

{

throw
s;
}

catch
(exception &e)
{

cout<<"Exception message: "<<e.what()<<endl;
}

catch
(string &ss)
{

cout<<"Exception message: "<<ss.c_str()<<endl;
}

catch
(...)
{

cout<<"All exceptions not handled are caught here"<<endl;
}
}


//Throwing a class and catching it
void function4()
{

cout<<"\nfunction4()"<<endl;
string s = "function4() exception";
try

{

throw
zgEx;
}

catch
(exception &e)
{

cout<<"Exception message: "<<e.what()<<endl;
}

catch
(...)
{

cout<<"All exceptions not handled are caught here"<<endl;
}
}






The output is as follows: