Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Throw / Catch - Exception Handling 3

Status
Not open for further replies.

Skute

Programmer
Jul 21, 2003
272
GB
Hi,

Can someone give me a good example of throwing your own exception classes and catching a reference of them, ie:

try
{
functionx();
}
catch (MyException& ex)
{
printf(ex.GetMessage());
}

functionx()
{
// This isnt right!
MyException* pEx = new MyException();
pEx->SetMessage("Throwing a random message here...");
throw pEx;
}



Skute

"There are 10 types of people in this World, those that understand binary, and those that don't!"
 
ok, in your code you throw a pointer but catch a reference...
To throw and to catch a reference you should throw like this:
throw *pEx;

Ion Filipski
1c.bmp
 
yeah thanks, that was more of a typo by me, i meant, how can i do it in one line?

like

throw MyException();

which i have seen in other code, it looks like this:

....

throw CIMHTTPException();

......
catch (Exception& e)
{
ExitError(e.getMessage());
}

?

thanks

Skute

"There are 10 types of people in this World, those that understand binary, and those that don't!"
 
You can't do it in one line unless you can pass the required information to the exception class in the constructor. If MyException has a constructor that takes a string, you can do:
Code:
functionx()
{
  throw MyException("Throwing a random message here...");
}
otherwise you'll have to do:
Code:
functionx()
{
  MyException myEx;
  myEx.SetMessage("Throwing a random message here...");
  throw myEx;
}
 
yeah i was thinking more along the lines of using inherited exceptions, ones that have the text pre-defined (purely because it looked quicker and easier)

so you would have classes:

MyException
MyNullException
MyOutOfBoundsException


then,

throw MyNullException()

etc



Skute

"There are 10 types of people in this World, those that understand binary, and those that don't!"
 
I would not use
throw *(new MyNullException(...));

Who is going to free that memory? (This might be certain memory leak with an implicit copy during the throw, I'm not sure). Unless you have a fancy scheme so that you know the memory will be deleted, I'd stay away from creating exception objects on the heap. Just create it on the stack and throw it, then catch it by reference. You won't have to worry about memory management.
 
Skute wanted to create an exception in a single line, to an unknown at compile time an exception with unknown type(inherited from some basic exception interface) and to cath a reference :) That's the only way you can do that. But in my opinion it makes no sence. Much better is to throw a pointer, to catch a pointer and to delete it inside catch block. That's the way it is implemented in MFC, the macroses THROW/THROW_LAST/TRY/CATCH/END_CATCH/AND_CATCH/CATCH_ALL/AND_CATCH_ALL/END_CATCH_ALL.

Ion Filipski
1c.bmp
 
Actually, I don't think that's the only way you can do that.
Code:
try
{
  throw MyNullException();
}
catch (MyException& myEx)
{
}
should work just fine. And I just don't see the reason to throw/catch a pointer like MFC does it when there is no reason to do so. Why add extra memory management that can get messed up?
 
yeah thats the way i thought it would have worked but it keeps crashing during runtime :(

Thanks for both points, both interesting, which way do you personally opt for?

Skute

"There are 10 types of people in this World, those that understand binary, and those that don't!"
 
I usually do it somewhat like this:

Code:
class MyClass
{
public:
	class MyException
    {
	public:
		MyException(LPCTSTR desc):mDesc(desc) {}
		virtual ~MyException() {}

		void throwMe() { throw *this; }

		LPCTSTR getDescription() const { return mDesc; }
	private:
		CString mDesc;
	};
	...
private:
		void throwException(LPCTSTR desc)
		{
			MyException e(desc);
			e.throwMe();
		}
	...
}
...
void MyClass::someMethod()
{
	if (someError)
		throwException("Uh-oh!");
	...
}
...
void someCaller()
{
	MyClass c;

	try
	{
		c.someMethod();
	}
	catch (MyClass::MyException& e) // Note: Its a reference, no neeed to use pointer
	{
		cout << e.getDescription(); << endl;
	}
}

MyException can of course inhereit a more generic exception that defines the virtual throwMe and does all the work. In that case MyExpection doesnt do much, it'd just be used to catch the specific exception thrown.

In any case, the throwMe would be a good place to put a breakpoint if you're debugging....

/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
cheers, thanks alot. seems a reasonably straight forward method

Skute

&quot;There are 10 types of people in this World, those that understand binary, and those that don't!&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top