Results 1 to 8 of 8

Thread: More exception handling questions

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    More exception handling questions

    Hi guys,

    I'm still trying to make sure I am understanding structured exception handling correctly. I have a piece of code to demonstrate my question.

    Code:
            internal void ReadFromBinary(string PathToFile)
            {
                try
                {
                    fileAsStream = new FileStream(PathToFile, FileMode.Open);
                    fileReader = new StreamReader(fileAsStream);
                }
                catch (FileNotFoundException)
                {
                    throw; //Throw the up to the caller?
                }
    
                //Does any code from here down not get called unless the exception is handled?
            }

  2. #2
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: More exception handling questions

    the catch will throw the exception
    and if there is any codes will be added after the catch statement all these code will be called
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: More exception handling questions

    And if I do not want the code to be called? It hardley seems structured to use a bool and if block after the catch.

  4. #4
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: More exception handling questions

    you can use simple
    Code:
    return ;
    in your method return; will make you get out of the method when you enter the catch block
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: More exception handling questions

    Whaaat? Noooo.... once an exception is thrown, control is handed back to the calling error handler. So in the case posted, the throw in the catch will re-raise the error, forcing the call to the function to error. If there was an error handler around it, it will then go into that catch. If there isn't then the call to ReadFromBinary will error out. The exception will keep bubbling up the chain until there's code somewhere that handles it... code execution will then resume AT THAT POINT.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: More exception handling questions

    amm, lol sorry i'm the one how do the wrong
    i didn't see the throw; i think was wrong i must have a look for the question

    my answer was if he catch the error and handled it with out the throw


    sorry again
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: More exception handling questions

    Yeah thats what I thought but I wasn't sure of which point the code resumed at. Also Between those two lines of code numerous exceptions can occur. Can I simply just handle system.exception when I know that any exception rasied means the function should not continue?

  8. #8
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: More exception handling questions

    all the Exceptions comes from System.Exception

    if you have Exception such FileNotFoundException you can handle it with the way you are
    if you don't what the exception that will appear
    Code:
    catch(Exception e)
    {
    //// do what you want
    }
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width