I have the following code
try {
clientService.sin_family = AF_INET;
clientService.sin_addr = *((LPIN_ADDR)*hostE开发者_开发知识库ntry->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?
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.
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.
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).
精彩评论