6

In my c++ code, I have a block of code that gives me an "Access Violation Writing Location ..." Exception when user input is invalid..

I tried to catch this exception in my try/catch block to display error message when the exception occurs.. but for some reason it is not catching the error.

try {
    // ... some code that causes Access Violation Writing Location Exception
}
catch (...) {
    std::cout << "invalid user input" << endl;
}

I did this, but when the exception occurs, the console does not display my error message, but says there is an

Unhandled exception at 0x0F0B0E9A (msvcr110d.dll) in Example.exe : Access violation writing location

So it seems like my try/catch block is not catching the exception...

I set break points to make sure that the exception is occuring within the try block.. and I'm 100% that's the case..

Why is "catch (...)" not catching the Access Violation exception?

user3794186
  • 639
  • 2
  • 8
  • 10
  • 1
    That isn't a regular C++ exception that you can catch and handle. – chris Jul 14 '14 at 15:22
  • @chris so there's no way to catch and display error message? – user3794186 Jul 14 '14 at 15:22
  • If the exception is generated in the C++ redistributable and handled there (by displaying that message and doing whatever it pleases), there's not much you can do about it from a "sane user" perspective: if it's not propagated there's nothing wrong for you – Marco A. Jul 14 '14 at 15:23
  • 7
    @user3794186, Why not figure out why it's occurring in the first place and fix that? – chris Jul 14 '14 at 15:24
  • The comment above this one deserves a thousand +1s – Marco A. Jul 14 '14 at 15:25
  • @chris it occrus when the user enters an invalid input .. I can't just assume that the user will enter valid input all the time, so I was just trying to catch it whenever it occurs, instead of having my program crash – user3794186 Jul 14 '14 at 15:25
  • 1
    @user3794186, Please provide an [MCVE](http://stackoverflow.com/help/mcve) and sample input that you supposedly can't validate and we'll tell you how to validate it. – chris Jul 14 '14 at 15:27
  • @user3794186 _'it occrus when the user enters an invalid input'_ Validate the input then before doing anything with it! You can never rely on valid input given to your software. – πάντα ῥεῖ Jul 14 '14 at 15:43

1 Answers1

17

Don't do this!

An access violation is not a C++ exception. It is the operating system trying to terminate the application because it did something invalid.

Specifically, it tried to write to a memory address it did not have the privileges to access. This basically means you're writing to random memory, which means that even if you did catch this error and showed a nice error message to the user, it might not always work. Sometimes, instead of writing to memory that you don't have write permissions for, the program might end up writing over other parts of your program. That won't raise an access violation, so the problem won't be detected. It will just corrupt your program.

The only correct way to do this is to validate your user input. You have to check that the user input is in a form that your program can handle safely. If it isn't, you have to either correct it, or abort, and show an error to the user. You have to do that in your own code, before your application does something so bad that the OS is forced to try to terminate it.

Yes, there is a way to handle Access Violations, but as I said above, that is not the correct solution to your problem, so I see no reason to elaborate on it.

jalf
  • 243,077
  • 51
  • 345
  • 550
  • 4
    +1000000 for the detailed explanation & reason, instead of just writing a single line comment telling me to simply "fix it" – user3794186 Jul 14 '14 at 15:36
  • @jalf Hello, I was wondering if you could elaborate on my question https://stackoverflow.com/questions/50165795/c-make-a-program-write-over-itself and what you said here mainly the part where writeing over your program wont raise an access violation! Thank you! :) – The Floating Brain May 04 '18 at 01:29