Exception errors OR exception handling :S

This is a discussion on Exception errors OR exception handling :S within the C++ Programming forums, part of the General Programming Boards category; I got this new book to help teach C++ and it's been going great, absolutly flying through it and no ...

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

    Exception errors OR exception handling :S

    I got this new book to help teach C++ and it's been going great, absolutly flying through it and no probs with understanding it, till today :S

    I'm up to a part in this book that talks about exception errors or handling, from what i gather you use the code in places you think will have errors :S no really i don't have a clue, well atleast i should be treated this way and if someone could just help me wrap my head around it that would be awsome

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Are you talking about try and catch blocks?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    9
    Quote Originally Posted by AndrewHunter View Post
    Are you talking about try and catch blocks?
    Yup, you got your try & catch blocks, thats it.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Think of it this way:
    You have an operation, such as copying and replacing a file.
    To ensure all goes well, you break it into parts:

    - You open the source file.
    - You open the (temporary) destination file.
    - You read a chunk from the source file.
    - You read the chunk to the destination file (and repeat until done).
    - You then rename the destination file you're replacing.
    - You then rename the temporary destination file to the name of the file you're replacing.
    - You then delete the renamed original file you were supposed to replace.

    But what if an error occurs somewhere in this chain?
    Perhaps you don't have permission to open or write the source or destination file. Perhaps there a read error or write error when reading/writing to the files.
    In this case, you might have to abort because you a subsequent task cannot execute until all of its previous tasks have been completing.
    This is where exceptions come in. Say a task fails. That task then throws an exception. This exception is then caught and an error reported.

    To be technical, the try block would surround all of these tasks, and the catch block would follow just after the try block.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    9
    Quote Originally Posted by Elysia View Post
    Think of it this way:
    You have an operation, such as copying and replacing a file.
    To ensure all goes well, you break it into parts:

    - You open the source file.
    - You open the (temporary) destination file.
    - You read a chunk from the source file.
    - You read the chunk to the destination file (and repeat until done).
    - You then rename the destination file you're replacing.
    - You then rename the temporary destination file to the name of the file you're replacing.
    - You then delete the renamed original file you were supposed to replace.

    But what if an error occurs somewhere in this chain?
    Perhaps you don't have permission to open or write the source or destination file. Perhaps there a read error or write error when reading/writing to the files.
    In this case, you might have to abort because you a subsequent task cannot execute until all of its previous tasks have been completing.
    This is where exceptions come in. Say a task fails. That task then throws an exception. This exception is then caught and an error reported.

    To be technical, the try block would surround all of these tasks, and the catch block would follow just after the try block.
    Yep i think that makes sense but from what you have just said i wont have to use them for some time so i'll practice on them laters i think.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling
    By Mr.Sellars in forum C++ Programming
    Replies: 16
    Last Post: 08-10-2007, 10:44 PM
  2. CIN and exception handling
    By OldSchool in forum C++ Programming
    Replies: 4
    Last Post: 05-14-2006, 05:02 PM
  3. Exception handling !!!
    By Brain Cell in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2005, 05:25 PM
  4. Exception handling
    By kuwait in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2003, 05:20 AM
  5. Exception handling
    By Hankyaku in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2003, 08:25 AM

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