1

In my program I pass an argument from console and save it to a variable. Let's say

const  string FileName= argv[1];

If there is no argument passing I get this

terminate called throwing an exception

How can I catch an exception and show proper error to user that there is no arguments passed?

Bernard
  • 4,240
  • 18
  • 55
  • 88

4 Answers4

3

argc gives you the size of argv, so check its value before accessing argv. Remember that argv is zero-based, so has bounds argv[0, ..., arrc - 1]. Helpfully, argv[argc] is always set to NULL.

Accessing an invalid element of argv is undefined behaviour.

As a final remark, argv[0] (if it isn't NULL) is the program name.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • (Actually, it is `[0, argc]`. The last is guaranteed to be `NULL`. See 3.6.1/2) – BoBTFish Sep 13 '13 at 07:31
  • Actually `argv[argc]` is a valid entry. It should also always be `NULL` (which is the problem for the OP, he's trying to assign a `NULL` pointer to a `std::string`). – Some programmer dude Sep 13 '13 at 07:31
  • Isn't the zero element of argv initialized in any case as it was echo? So 1 means you have 1 parameter in argv[1]? or I remember wrong? – dhein Sep 13 '13 at 07:33
  • 1
    @Zaibis The name the program was executed with goes in `argv[0]` (or, according to The Standard, `""` is allowed instead), but that is counted in `argc`. So if `argc` is `1`, the program was executed with no extra arguments. – BoBTFish Sep 13 '13 at 07:35
  • @BoBTFish, I wasn't aware of that. I've amended the answer. – Bathsheba Sep 13 '13 at 07:35
  • ah ok so argc will never be `argc < 1`? – dhein Sep 13 '13 at 07:36
  • @Zaibis, you ought not rely on this. Always check `argc` for safety; someone else may be using `main`. – Bathsheba Sep 13 '13 at 07:41
  • @Bathsheba Would you mind clarifying that? Do you mean things like [SDL, that may hijack `main` to do some initialisation](http://stackoverflow.com/q/11976084/1171191)? – BoBTFish Sep 13 '13 at 07:47
  • Well, so argc SHOULD be never less then 1? – dhein Sep 13 '13 at 07:48
  • @BoBTFish; indeed. I'm sure Windows 3.1 and earlier were particularly 'good' at hijacking the main function but can't recall the mechanism they used. But I am super-paranoid when it comes to overrunning array buffers; particularly in pure C. – Bathsheba Sep 13 '13 at 07:51
1

In your case you just should check the value of argc, as it holds the amount of parameters which are parsed to argv.

dhein
  • 6,431
  • 4
  • 42
  • 74
1

Your main has two arguments - argv and argc. While argv stores the arguments passed to your program argc stores their count. So you can check if an argument was provided to your program by checking the value of argc.

Keep in mind, though, that the first value in argv is the executable name, so if you want to check that a argument was passed to your program you should have a check like:

if (argc >= 2) {
  ... do stuff ...
}
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
0

You should check argc as in:

const  string FileName= (argc < 2)?string():argv[1];

As for your specific question, you catch exceptions by means of a try/catch block as in

try{
exceptionThrowingOperation();
}
catch( ThrownExceptionType& e ){
exceptionHandling(e);
}
Stefano Falasca
  • 8,837
  • 2
  • 18
  • 24