exception Handling

This is a discussion on exception Handling within the C++ Programming forums, part of the General Programming Boards category; Hi, I need to include into my program exception handling on my fuction/objects in my program but am unsure on ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    12

    Exclamation exception Handling

    Hi, I need to include into my program exception handling on my fuction/objects in my
    program but am unsure on how to go about this, i looked on the information that has
    been given on this site and still am no closer to solving the issue, please could
    anybody explain this too me or if possible show me on sum coding that i have
    placed below:
    Code:
    void ViewPublication::OnBnClickedButton6()
    {
    	Node *viewPublication =  mylist.end();
    	
    	
    	publisher.SetWindowTextW((*viewPublication).publication.Add_publisher);
    	isbn.SetWindowTextW((*viewPublication).publication.Isbn);
    	view_type.SetWindowTextW((*viewPublication).publication.Type);
    	title1.SetWindowTextW((*viewPublication).books.title1);
    	author.SetWindowTextW((*viewPublication).books.author);
    	title2.SetWindowTextW((*viewPublication).Title.title1);
    	editor.SetWindowTextW((*viewPublication).probooks.editor);
    	date.SetWindowTextW((*viewPublication).probooks.date);
    	
    }
    or if this makes things easier:

    Code:
    Node* List::end()
    
    
    {
    	Node *Navigator =_head; 
    
    	while ((*Navigator).next !=NULL)
    	{
    		Navigator = (*Navigator).next;
    		_last=Navigator;
    	}
    
    	_current=_last;
    	return _last;
    }
    Thanks
    Last edited by 1troy123; 08-25-2011 at 06:53 PM.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    There are two parts to proper exception handling: letting the stack unwind, and stopping it at the right time.

    Letting the stack unwind means that you want to make it so that the stack can unwind without leaking resources. Usually this is achieved by wrapping your resources in objects that will release the resource when the object is destroyed.

    Stopping it at the right time means catching the exception where you can do something about it. If there is any opportunity to retry, or prompt the user to ask again after fixing external problems, these are ideal spots. Logging fatal errors can is also be a reason to catch the exception, in this case near the top level (like in main).
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    12
    do have any idea how i would include this within my code? all i kno is that i need to include three things try, catch and throw, however i dont kno how to use them in my coding, if you can please help, thanks

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    12
    okay i have this so far can anybody please help me with the following error:error C2310: catch handlers must specify one type......or any that you might see happen
    Code:
    try{
    {
    	Node *Navigator =_head; 
    
    	while ((*Navigator).next !=NULL)
    	{
    		Navigator = (*Navigator).next;
    		_last=Navigator;
    	}
    
    	_current=_last;
    	return _last;
    }
    }
    catch() 
    {
    AfxMessageBox (_T("Error"));
    	
    }
    thanks

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Are you working through a book or tutorial to learn C++? Or if you are in a class, have you read your relevant notes?

    If so, you would have seen examples such as:
    Code:
    try
    {
        // code that may throw/propagate an exception derived from std::exception
        // ...
    }
    catch (const std::exception& e)
    {
        // ...
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    12
    thanks for the help i did see somthing that looked simular, would you say that this right, i appreiate the help so much i am truely grateful
    Code:
    try{
    {
    	Node *Navigator =_head; 
    
    	while ((*Navigator).next !=NULL)
    	{
    		Navigator = (*Navigator).next;
    		_last=Navigator;
    	}
    
    	_current=_last;
    	return _last;
    }
    }
    catch(const std::exception& e) 
    {
    AfxMessageBox (_T("Error"));
    	
    }

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    12
    i found that this dosnt work my code crashes with it aswell as without it

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Not likely, because the code within the try block does not look like it will throw or propagate an exception.

    I think that you need to get an overview of what exceptions are about before you try to use them.

    EDIT:
    Quote Originally Posted by 1troy123
    i found that this dosnt work my code crashes with it aswell as without it
    That indicates a problem with your code, probably a logic error leading to undefined behaviour. Exception handling is not appropriate to fix that, though it could sometimes help you diagnose such problems.
    Last edited by laserlight; 08-25-2011 at 09:36 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    12
    i have to include some exception handling into my project and not sure where to put them, i have functions in my code that are under buttons and that was the only place i could think of putting them if u can help please do if im way off thenim sorry for wasting ur time, thanks

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The only flaw I see in the code is possibly following a null pointer, should _head be equal to NULL (since you never check for such a thing). But that's not the kind of thing that throws an exception that I know of. Have you looked at your documentation as to what actually causes exceptions? Or perhaps you are supposed to be checking for NULL and throwing an logic_error exception in that case. You will probably get more guidance from the mysterious "they" who are forcing you to use exceptions, as presumably they have told (or will tell) you what kind of exceptions they want caught.

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    12
    As i need some sort of exception handling in my project would it be possible for anybody to give me an idea of what i could possibley use exception handling for, i have a program that allows a user to input and store data into an external file, load data from said file and browse, delete and search for items in that file (using node and list), please if anybody can help!

  12. #12
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    As i need some sort of exception handling in my project
    Can you explain why you need exception handling? Do you have a problem that needs to be fixed by it? If so, post code. Do you want to use exception handling to throw your own exceptions or just catch exceptions thrown by library functions? If so, what library?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  2. Exception handling
    By Mr.Sellars in forum C++ Programming
    Replies: 16
    Last Post: 08-10-2007, 10:44 PM
  3. CIN and exception handling
    By OldSchool in forum C++ Programming
    Replies: 4
    Last Post: 05-14-2006, 05:02 PM
  4. ATL exception handling
    By rzcodeman in forum Windows Programming
    Replies: 1
    Last Post: 06-10-2004, 06:19 PM
  5. Exception handling
    By kuwait in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2003, 05:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21