473,385 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Exception Un Caught in C++

Hi

Expand|Select|Wrap|Line Numbers
  1. int i = 0;
  2. try
  3. {
  4.    MyCls as(param1, param2)
  5.    i = as->kpy->size();
  6. }
  7. catch(CError &)
  8. {
  9. }
  10.  
if as is having null value then why catch block is
not catching Null exception thrown ?

Will a null Exception be thrown in try block ?

Can anyone explain this ?

Don't tell me about catch(...), which catches all the
exception. Please clear me with the situation which is
mentioned in the code.

Please reply to the earliest.

Regards,
Girish.
Aug 24 '07 #1
2 1260
Banfa
9,065 Expert Mod 8TB
The problem is that NULL pointer and invalid memory accesses (segmentation faults) are not generated by the program (like normal exceptions) but are generated externally by the system which signals the program.

If you want to catch them you have to intercept the signal from the system and handle it something like this

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <signal.h>
  4.  
  5. using namespace std;
  6.  
  7. void SEGV_handler(int i)
  8. {
  9.     cout << "Oops" << endl;
  10.     throw string("SEGV");
  11. }
  12.  
  13. int main(int argc, char **argp)
  14. {
  15.     char *p = 0;
  16.  
  17.     signal(SIGSEGV, SEGV_handler);
  18.  
  19.     try
  20.     {
  21.         *p = 'a';
  22.     }
  23.     catch(string s)
  24.     {
  25.         cout << "Exception Caught: " << s << endl;
  26.     }
  27.  
  28.     return 0;
  29. }
  30.  
Notice that I catch a string because I throw a string.

Look up signal.
Aug 24 '07 #2
The problem is that NULL pointer and invalid memory accesses (segmentation faults) are not generated by the program (like normal exceptions) but are generated externally by the system which signals the program.

If you want to catch them you have to intercept the signal from the system and handle it something like this

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <signal.h>
  4.  
  5. using namespace std;
  6.  
  7. void SEGV_handler(int i)
  8. {
  9.     cout << "Oops" << endl;
  10.     throw string("SEGV");
  11. }
  12.  
  13. int main(int argc, char **argp)
  14. {
  15.     char *p = 0;
  16.  
  17.     signal(SIGSEGV, SEGV_handler);
  18.  
  19.     try
  20.     {
  21.         *p = 'a';
  22.     }
  23.     catch(string s)
  24.     {
  25.         cout << "Exception Caught: " << s << endl;
  26.     }
  27.  
  28.     return 0;
  29. }
  30.  
Notice that I catch a string because I throw a string.

Look up signal.
Thanks a lot Banfa, You gave me the exact answer for which
I was trying all the time in Google.

It really helped me a lot. Thanks again.

Regards,
Girish.
Aug 24 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Jacek Dziedzic | last post by:
Hi! In my main() function I have a last-resort exception construct that looks like this: int main() { try { // ... program code }
1
by: Chris Fink | last post by:
I have written a c# windows application which is occasionally throwing this messagebox popup exception in the test environment: "application has generated an exception that could not be handled"...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
6
by: Vadivel Kumar | last post by:
I've a problem in handling a custom exception The following is my custom exception class: public class AppException : public Exception { public AppException (string message, Exception...
2
by: James Cooke | last post by:
Hi all, I want to catch a duplicate key exception. I do not want to provide that verbose message from the MSSQL server - I would put a user friendly message out, like "The item you have added...
4
by: Rob Richardson | last post by:
Greetings! I am working on an application that targets a Pocket PC running Windows CE and SQL Server CE. Almost all functions in the application use a Try block with a Catch block that looks...
11
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
6
by: daniel | last post by:
I use a simple program to illustrate the problem: import logging def foo() : raise ValueError("foo") if __name__ == "__main__" : try : foo()
12
by: josh | last post by:
Hi I have this code: class Exception : public exception { public: Exception(string m="exception!") : msg(m) {} ~Exception() throw() {} const char* what() { return msg.c_str(); } private:
10
by: Rahul | last post by:
Hi Everyone, I have the following exception class, class E1 { }; class E2 {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.