exceptions [modified]
-
hi again :) I saw in some code such thing : (it was a mathematical formula parser)
try
{
...
}
catch(CException1 pError)
{
...
}
catch(CException2 pError)
{
...
}
catch(CException3 pError)
{
...
}catch(CException4 pError)
{
...
}
catch(CException5 pError)
{
...
}
catch(CException6 pError)
{
...
}I have question : Is not it uncomfortable to write so many catches ? when all theses exceptions are some what related, could not we unite them under some
CMainException
class and catch only one exception ? this code is from this article : http://www.codeproject.com/cpp/VisualCalc.asp From the Using the Code section But the article is verry coool :rose: -- modified at 4:15 Monday 29th May, 2006 -
hi again :) I saw in some code such thing : (it was a mathematical formula parser)
try
{
...
}
catch(CException1 pError)
{
...
}
catch(CException2 pError)
{
...
}
catch(CException3 pError)
{
...
}catch(CException4 pError)
{
...
}
catch(CException5 pError)
{
...
}
catch(CException6 pError)
{
...
}I have question : Is not it uncomfortable to write so many catches ? when all theses exceptions are some what related, could not we unite them under some
CMainException
class and catch only one exception ? this code is from this article : http://www.codeproject.com/cpp/VisualCalc.asp From the Using the Code section But the article is verry coool :rose: -- modified at 4:15 Monday 29th May, 2006Catch(...)
and thenGetLastError()
:rose: -
Catch(...)
and thenGetLastError()
:rose:GetLastError will not help u in all cases. it applicable if only if the library set the relevant errors u may have to design ur own class if the exception is not provded by windows or the library u are using. if it is a generic one, then the code will be very less. there are centralized and decentralized error handling strategy. select one which suits for u. if u r trying to reduce code, go for centralized one. but the common routine or class will be bulky because of this :( SaRath
-
hi again :) I saw in some code such thing : (it was a mathematical formula parser)
try
{
...
}
catch(CException1 pError)
{
...
}
catch(CException2 pError)
{
...
}
catch(CException3 pError)
{
...
}catch(CException4 pError)
{
...
}
catch(CException5 pError)
{
...
}
catch(CException6 pError)
{
...
}I have question : Is not it uncomfortable to write so many catches ? when all theses exceptions are some what related, could not we unite them under some
CMainException
class and catch only one exception ? this code is from this article : http://www.codeproject.com/cpp/VisualCalc.asp From the Using the Code section But the article is verry coool :rose: -- modified at 4:15 Monday 29th May, 2006big_denny_200 wrote:
this code is from this article : http://www.codeproject.com/cpp/VisualCalc.asp
It would be better if you ask the author of article directly, by posting your query at bottom of article only! anyways you can make a Base class
CMainException and derived rest of class (i.e. CException1 ...) from them! and you can catch the any Exception in Base class Object
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You
-
Catch(...)
and thenGetLastError()
:rose:Aljechin wrote:
GetLastError()
you can't get any error code or string using GetLastError if the Exception throwed is Custom!, GetLastError is Window specific api!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You
-
Catch(...)
and thenGetLastError()
:rose:In general
catch(...)
is bad form. There are exceptions such as when you do a catch all, perform some clean up and then re-throw, for example. A general rule of thumb with exception handling is that you should only catch what you expect to be thrown; a catch all violates this principle. Steve -
hi again :) I saw in some code such thing : (it was a mathematical formula parser)
try
{
...
}
catch(CException1 pError)
{
...
}
catch(CException2 pError)
{
...
}
catch(CException3 pError)
{
...
}catch(CException4 pError)
{
...
}
catch(CException5 pError)
{
...
}
catch(CException6 pError)
{
...
}I have question : Is not it uncomfortable to write so many catches ? when all theses exceptions are some what related, could not we unite them under some
CMainException
class and catch only one exception ? this code is from this article : http://www.codeproject.com/cpp/VisualCalc.asp From the Using the Code section But the article is verry coool :rose: -- modified at 4:15 Monday 29th May, 2006It is better to catch exceptions by reference rather then by value as is in your sample code. i.e.
try { .. } catch(const CException1 & Error) { ... } // More stuff goes here.
First it's more efficient as no copying occurs. More importantly it avoids slicing and thus allows you to make a catch class withvirtual
functions. This is one way to avoid catching so many different exceptions types: instead you catch a base class withvirtual
functions by reference and make use of polymorphism. Steve -
hi again :) I saw in some code such thing : (it was a mathematical formula parser)
try
{
...
}
catch(CException1 pError)
{
...
}
catch(CException2 pError)
{
...
}
catch(CException3 pError)
{
...
}catch(CException4 pError)
{
...
}
catch(CException5 pError)
{
...
}
catch(CException6 pError)
{
...
}I have question : Is not it uncomfortable to write so many catches ? when all theses exceptions are some what related, could not we unite them under some
CMainException
class and catch only one exception ? this code is from this article : http://www.codeproject.com/cpp/VisualCalc.asp From the Using the Code section But the article is verry coool :rose: -- modified at 4:15 Monday 29th May, 2006hi denny, why didn't you ask this at the bottom of the VisualCalc[^] article ? i would have asked immediately... for your question, note that i don't catch the exceptions like you do (by copy :
catch(CException1 pError)
) but by reference (catch(CException1& pError)
) which is trully different for the memory management. about the many catch blocs, there's no problem with that. you can have as many catch as you like without altering the performances... you can however reduce the number of catches (design matter) as i provide a base class for all the Parser exceptions (CVCAlcParserException
class), but by doing this, you wouldn't be able to tell the user if he gets a syntax, a mathematic, or whatever kind of error... that's all i think. if you have any other questions, don't hesitate to ask on the article's message board ;)
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
Catch(...)
and thenGetLastError()
:rose:VisualCalc provides its own exception classes, so you will never get any accurate error message. moreover, be careful of the case sensitivity of the language.
**C**atch
is not a valid keyword
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
big_denny_200 wrote:
this code is from this article : http://www.codeproject.com/cpp/VisualCalc.asp
It would be better if you ask the author of article directly, by posting your query at bottom of article only! anyways you can make a Base class
CMainException and derived rest of class (i.e. CException1 ...) from them! and you can catch the any Exception in Base class Object
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You
ThatsAlok wrote:
you can make a Base class CMainException and derived rest of class from them
like
CVCalcParserException
base class for example ? ;)
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
It is better to catch exceptions by reference rather then by value as is in your sample code. i.e.
try { .. } catch(const CException1 & Error) { ... } // More stuff goes here.
First it's more efficient as no copying occurs. More importantly it avoids slicing and thus allows you to make a catch class withvirtual
functions. This is one way to avoid catching so many different exceptions types: instead you catch a base class withvirtual
functions by reference and make use of polymorphism. SteveStephen Hewitt wrote:
It is better to catch exceptions by reference rather then by value as is in your sample code
VisualCalc actually catches its exceptions by reference. the OP typed it wrong.
Stephen Hewitt wrote:
it [...] allows you to make a catch class with virtual functions
exactly what it does...
Stephen Hewitt wrote:
This is one way to avoid catching so many different exceptions types
it is also. actually, VisualCalc Parser provides a base class for all its exception classes, but i still catch each exception category one by one to provide a visual feedback to the calculator user... like this, he finally know if he did a syntax, mathematic of whatever kind of error in his expression...
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
ThatsAlok wrote:
you can make a Base class CMainException and derived rest of class from them
like
CVCalcParserException
base class for example ? ;)
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
toxcct wrote:
ike CVCalcParserException base class for example ? ;)
Thats why i asked him to post your problem in respective article Forum!,
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You