coolface
coolface

Reputation: 137

try / catch block fails and application crashes

I have the following code

try { 
    clientService.sin_family = AF_INET;
    clientService.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list); //fails
    clientService.sin_port = htons(port);
    }

catch (...) { return; }

If I disable my network adapter, my application crashes. Isn't such errors supposed to be caught?

Upvotes: 1

Views: 2078

Answers (3)

Steve Townsend
Steve Townsend

Reputation: 54138

It's difficult to reach a firm conclusion here without more code, but I imagine that hostEntry or its h_addr_list field is NULL due to the network being unavailable. Your code only handles C++ exceptions and you would need to enhance it to handle structured exceptions (such as access violation) in order for this to do what you expect.

Preferably, just fix the code so it does not use invalid pointers after an earlier error on gethostbyaddr et al (you should probably do this anyway, regardless of any exception handling enhancements).

Upvotes: 1

terminus
terminus

Reputation: 14462

It's just a guess but you probably haven't checked hostEntry after gethostbyname returned and you got a NULL-pointer. Check it if it's null before using it.

Upvotes: 1

Puppy
Puppy

Reputation: 146910

If you don't set VS right, then it will not catch SEH exceptions like access violations, although it IS possible to set it to catch SEH exceptions in catch(...) blocks. It's probably just a better idea to not exhibit the problem in the first place, SEH exceptions are usually an indicator that you did something seriously wrong.

Upvotes: 1

Related Questions