Exceptions, Throwing, Handling
-
I have application that uses a serial port and I/O card. The application starts up add attempts to initializes the serial card and I/O card. If either of them fail and would like to throw an exception telling the user that corresponding object failed and terminate the applications. How do i accomplish this? I have tried to use throw (" Unable to initialize Serial Port" ); However, it does not display my string. It does terminate the application but, the user is left clue unless..
Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures
-
I have application that uses a serial port and I/O card. The application starts up add attempts to initializes the serial card and I/O card. If either of them fail and would like to throw an exception telling the user that corresponding object failed and terminate the applications. How do i accomplish this? I have tried to use throw (" Unable to initialize Serial Port" ); However, it does not display my string. It does terminate the application but, the user is left clue unless..
Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures
If you want to implement something quick & throw just strings you can use std::exception
void main()
{
try
{
std::exception ex("My error");
throw ex;}
catch(std::exception& ex)
{
cout<< ex.what();
}}
The best way is to design your own exception class to deal with different cases -or even a struct, As C++ is quite flexible when it comes to throwing something ;)
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
-
I have application that uses a serial port and I/O card. The application starts up add attempts to initializes the serial card and I/O card. If either of them fail and would like to throw an exception telling the user that corresponding object failed and terminate the applications. How do i accomplish this? I have tried to use throw (" Unable to initialize Serial Port" ); However, it does not display my string. It does terminate the application but, the user is left clue unless..
Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures
Oops sorry. I was too quick to respond. If you want to catch any exception and then throw a meaningful one , you should do like
void IOReadFuntion()
{
try
{
// IO functions.
}
catch(...)
{
std::exception ex("Error while reading port");
throw ex;
}
}void main()
{
try
{
IOReadFuntion();
IOWriteFunction();}
catch(std::exception& ex)
{
cout<<ex.what()
}}
Signing off. good night! :zzz:
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
-
Oops sorry. I was too quick to respond. If you want to catch any exception and then throw a meaningful one , you should do like
void IOReadFuntion()
{
try
{
// IO functions.
}
catch(...)
{
std::exception ex("Error while reading port");
throw ex;
}
}void main()
{
try
{
IOReadFuntion();
IOWriteFunction();}
catch(std::exception& ex)
{
cout<<ex.what()
}}
Signing off. good night! :zzz:
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
Vunic, I am not using a console application. I have MFC application and when i throw and exception it creates window that say Microsft Visual C++ Runtime Error. It has big red x and Has the path of the exe that failed, along with a string telling the failure. I wanna use my string instead of the generic string provided.
Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures
-
I have application that uses a serial port and I/O card. The application starts up add attempts to initializes the serial card and I/O card. If either of them fail and would like to throw an exception telling the user that corresponding object failed and terminate the applications. How do i accomplish this? I have tried to use throw (" Unable to initialize Serial Port" ); However, it does not display my string. It does terminate the application but, the user is left clue unless..
Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures
Why use an exception? Why not just attempt to initialize the serial card and return an error code on failure? I actually have code that does exactly that (our system uses a lot of serial ports.) Most our code doesn't use MFC, but the few programs that do, I do this check in the InitInstance() function. On failure, it pops up a message box explaining the failure. (For some applications, I return a resource ID of the error string; in others just a bool indicating success or failure--it all depends on the requirements.)
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
Vunic, I am not using a console application. I have MFC application and when i throw and exception it creates window that say Microsft Visual C++ Runtime Error. It has big red x and Has the path of the exe that failed, along with a string telling the failure. I wanna use my string instead of the generic string provided.
Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures
ScotDolan wrote:
when i throw and exception it creates window that say Microsft Visual C++ Runtime Error.
No way, when you throw your own exception. And if you are handling it, it should work. Try this one. No matter it is console or dialog.
try { std::exception ex("MyExeception"); throw ex; } catch(std::exception& ex) { AfxMessageBox(ex.what()); }
btw, if you want to catch any unhandled exception, you could try
SetUnhandledExceptionFilter()
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
-
I have application that uses a serial port and I/O card. The application starts up add attempts to initializes the serial card and I/O card. If either of them fail and would like to throw an exception telling the user that corresponding object failed and terminate the applications. How do i accomplish this? I have tried to use throw (" Unable to initialize Serial Port" ); However, it does not display my string. It does terminate the application but, the user is left clue unless..
Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures
I'd second Joe's reply - why on earth are you using exceptions for this. A serial port being unavailable is by its nature a normal thing. Not _exception_al at all! Why not:
BOOL CMyDialog::InitialisePort (UINT nComPort, CString &Error)
{
...
if (!...)
{
Error = _T(" Unable to initialize Serial Port" );
return FALSE;
}
...
return TRUE;
}? I can't think of a single place I've written a throw command. I've written a few catchers for other people's libraries, so I'm not a exception virgin - just not a fan. Good luck with your application, Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...