Results 1 to 17 of 17

Thread: [RESOLVED] Getting error and cannot seem to TRY/CATCH it

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Resolved [RESOLVED] Getting error and cannot seem to TRY/CATCH it

    I am getting this error

    First-chance exception at 0x0f7247b5 (StringLibrary.dll) in Librarian.exe: 0xC0000005: Access violation reading location 0x02e028b8.

    And I assume it's in this piece of code - as I can set a break at the start of the routine - and it never arrives at the bottom of the routine.

    I have never used TRY/CATCH statements - but why wouldn't this work - why is it not breaking in the CATCH?

    Code:
    			try {
    				int s1 = nKeywordskt;
    				int s2 = s1;
    				int s3 = 0, s4 = 0, s5 = 0, s6 = 0;
    				int a = 0;
    .
    .
    .
    				s1 = 0; // dummy line
    			} catch (int errCode) {
    				errCode = errCode;
    			}
    I set a break at this line int s1 = nKeywordskt and at the s1 = 0; // dummy line and also at the errCode = errCode line.

    It breaks at the top - and never arrives at the bottom (getting the error noted at the top of this post instead).

    But why does it not arrive in the CATCH??
    Last edited by szlamany; Sep 13th, 2012 at 05:07 PM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Getting error and cannot seem to TRY/CATCH it

    In the case of errors like that, no exceptions are thrown so there is nothing to catch.

    However, I cant see anything in your code that could cause an access violation, which generally occurs when you are trying to access memory that is not yours. Uninitialized pointers or pointer arithmetics gone bad can be causes for that error.
    Here is an example of an access violation:

    Code:
    int a = 5;
    int b = 10;
    
    int *p_a = &a;
    int *p_b = 0;
    
    int result = *p_a + *p_b; // Trying to use incorrectly initialized pointer p_b.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Getting error and cannot seem to TRY/CATCH it

    The ... in the code represents me cutting out the SHELL sort where I'm alphabettically sorting a pointer array that points to words in a string buffer.

    I know I'm doing something evil to memory - I just wanted to CATCH it so I could find it more "easily".

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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

    Re: Getting error and cannot seem to TRY/CATCH it

    It's because your catch syntax is off... you catch exceptions... not codes...
    http://msdn.microsoft.com/en-us/library/0yd65esw.aspx
    Quote Originally Posted by msdn
    Although the catch clause can be used without arguments to catch any type of exception, this usage is not recommended. In general, you should only catch those exceptions that you know how to recover from. Therefore, you should always specify an object argument derived from System.Exception
    it should look something like this:
    Code:
    catch (InvalidCastException e) 
    {
    // error handling here
    }
    you can also catch multiple exceptions
    Code:
    catch (FileNotFoundException e)
    {
        // FileNotFoundExceptions are handled here.
    }
    catch (IOException e)
    {
        // Extract some information from this exception, and then 
        // throw it to the parent method.
        if (e.Source != null)
            Console.WriteLine("IOException source: {0}", e.Source);
        throw;
    }
    * 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??? *

  5. #5

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Getting error and cannot seem to TRY/CATCH it

    That is C# - does it also go for C++

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Getting error and cannot seem to TRY/CATCH it

    tg, I think you are looking at the C# documentation. It is possible to throw and catch types that are not exceptions. For instance, this is valid:

    Code:
    	try
    	{
    		throw "foo";
    	}
    	catch(const char *message)
    	{
    		std::cout << "Caught: " << message;
    	}
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Getting error and cannot seem to TRY/CATCH it

    So CATCH can only be used when you do your own THROW - is it that kind of construct?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Getting error and cannot seem to TRY/CATCH it

    Quote Originally Posted by szlamany View Post
    So CATCH can only be used when you do your own THROW - is it that kind of construct?
    Yes that is right.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

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

    Re: Getting error and cannot seem to TRY/CATCH it

    Quote Originally Posted by szlamany View Post
    That is C# - does it also go for C++
    My bad... I thought we were in the C# area...

    Quote Originally Posted by Atheist View Post
    tg, I think you are looking at the C# documentation. It is possible to throw and catch types that are not exceptions. For instance, this is valid:

    Code:
    	try
    	{
    		throw "foo";
    	}
    	catch(const char *message)
    	{
    		std::cout << "Caught: " << message;
    	}
    I was... didn't realize this was in the C/C++ area...

    Sure that's valid.. but that can't be the only way catch works, can it? otherwise what would be the point?

    -tg


    edit - hey... does this help? http://www.cplusplus.com/doc/tutorial/exceptions/
    * 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??? *

  10. #10

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Getting error and cannot seem to TRY/CATCH it

    That link talked about

    Catch (...)

    using the "..." to catch the default error condition...

    But that still did not work - it will not break in that CATCH statement.

    Oh well...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  11. #11
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Getting error and cannot seem to TRY/CATCH it

    Quote Originally Posted by techgnome View Post
    Sure that's valid.. but that can't be the only way catch works, can it? otherwise what would be the point?
    http://www.cplusplus.com/doc/tutorial/exceptions/
    Im not sure what you mean by that, but the exception handling works similarly to .NET. You can define your own exception and throw it, and the first appropriate catch clause will handle the exception.

    Code:
    #include <iostream>
    #include <exception>
    
    class fooexception : public std::exception
    {
    public:
    
    	fooexception(const char *message) : std::exception(message)
    	{
    	}
    };
    
    int main(int argc, char **argv)
    {
    	try
    	{
    		throw new fooexception("Oh no!");
    	}
    	catch(fooexception *ex)
    	{
    		std::cout << "Caught fooexception: " << ex->what();
    	}
    	catch(std::exception *ex)
    	{
    		std::cout << "Caught std exception: " << ex->what();
    	}
    	
    
    	// getchar here just to get an opportunity to look at the output.
    	getchar();
    
    	return 0;
    }
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  12. #12

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Getting error and cannot seem to TRY/CATCH it

    From what I just researched things like access violations are more of a hardware exception. If AV's were by default "catchable" then it would be a huge performance hit.

    On top of the fact that an AV is not safe to CATCH and continue - I've obviously attempted to do a bad memory thing and was caught...

    I really need to give up on trying to debug why this latest attempt at adding new logic is flawed and instead go back to a version of this routine that worked a week ago and attempt to add the logic again but using some safer approaches (I was getting a bit carried away with working the arrays that VB.Net is giving me from the managed-side of this app).

    I'm going to mark this thread resolved - thanks for the lessons!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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

    Re: Getting error and cannot seem to TRY/CATCH it

    Quote Originally Posted by Atheist View Post
    Im not sure what you mean by that, but the exception handling works similarly to .NET. You can define your own exception and throw it, and the first appropriate catch clause will handle the exception.

    Code:
    #include <iostream>
    #include <exception>
    
    class fooexception : public std::exception
    {
    public:
    
    	fooexception(const char *message) : std::exception(message)
    	{
    	}
    };
    
    int main(int argc, char **argv)
    {
    	try
    	{
    		throw new fooexception("Oh no!");
    	}
    	catch(fooexception *ex)
    	{
    		std::cout << "Caught fooexception: " << ex->what();
    	}
    	catch(std::exception *ex)
    	{
    		std::cout << "Caught std exception: " << ex->what();
    	}
    	
    
    	// getchar here just to get an opportunity to look at the output.
    	getchar();
    
    	return 0;
    }
    What I was commenting about was the comment about the only way a catch works is if you throw it...
    Yes, it's valid... but then this transpired...
    szlamany asked "So CATCH can only be used when you do your own THROW " ... and you replied with "Yes that is right." ...
    which made me question what good is the try catch if it only catches exceptions YOU throw?

    -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??? *

  14. #14
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [RESOLVED] Getting error and cannot seem to TRY/CATCH it

    Ah I see. Well I assumed szlamany did not literally mean "throw statements that he himself has written" but rather "explicit throw statements in the code". My bad, I should have been clearer.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  15. #15

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] Getting error and cannot seem to TRY/CATCH it

    Ok - just to clarify all this...

    1) You can do your own THROW's (you all have shown that in examples above)
    2) THROW's happen in other places - functions and layers below you
    3) CATCH will only catch a "matching" type - CHAR or INT - basically it's based on TYPE DECLARATION
    4) Things like AV's are not CATCH-able

    Counter-point to #4 - it seems there are some VS IDE project settings that might get it to do an IDE break when the AV error happens - not sure how safe it is to debug around at that moment anyway. I've seen that this type of AV error is so nasty that I usually have to exit VS and re-start it to get good runs afterwards...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  16. #16
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [RESOLVED] Getting error and cannot seem to TRY/CATCH it

    I am not aware of a windows equivalent, but if you ever do some c++ on linux you should really try Valgrind. It is absolutely invaluable for debugging memory issues.

    I know its probably not applicable in this situation, just wanted to mention it
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  17. #17

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] Getting error and cannot seem to TRY/CATCH it

    My goal is to avoid memory issues by carefully stepping through new code and testing "start" of loop and "end" of loop and buffer offset calculations really, really carefully. I cannot be burned by this code - it's going to run in a background service and cannot fail.

    That's why I don't mind going back 20 hours in coding to a stable point and re-implementing this new logic with a better and safer approach.

    Reading through 500,000 bytes of text and doing word discovery and complex word pattern discovery has been both painful and rewarding.

    I am actually finding that I really like C++

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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