Click here to Skip to main content
15,894,271 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi there,

please find the below code,

C++
char l_Title_ca[10];
try{
	 sprintf_s(l_Title_ca,"Version %d.%02d [MemberId :- %s]",Ver,Rel,MemID);
 }
catch(char* str)
{
     cout << "error: ";
    // if(errn == BUFFER_TOO_SMALL)
     {
          cout << "storage buffer too small.\n";
          return 0;
     }
}catch(...)
{
	cout<<"caught..."<<endl;
} 


while executing the code at runtime, i am getting the error message "buffer too small" and not able to catch the exception using above method.

Please any can guide me how can i catch the exception?

Regards,
Ranjith
Posted
Updated 10-Dec-13 21:43pm
v3
Comments
Philippe Mori 11-Dec-13 23:37pm    
As explained by other, no exceptions are thrown... Also, you should fix the real problem which is that the buffer is too small.

hi,
the second argument of sprintf_s should be some integer value. Take a look here for example[^]

hope this helps !
 
Share this answer
 
Comments
ranjithkumar81 11-Dec-13 1:13am    
that is correct, actually without any changes in the code, i need to handle the exception.
chandanadhikari 11-Dec-13 1:54am    
sprintf_s returns -1 if errors occur (see the 'Return value' section in the link). maybe checking for this return value might help you but then again that will involve some code change. may i know why you do not want to use sprintf_s as recommended .
CPallini 11-Dec-13 3:48am    
"actually without any changes in the code"
Why? The code is apparently wrong.
ranjithkumar81 11-Dec-13 4:14am    
good question.

currently application is running on production , i can not touch the code to do any changes
CPallini 11-Dec-13 4:23am    
However you could add an exception handler...
sprintf_s() is not throwing exceptions. Instead the Invalid Parameter Handler[^] is called when the buffer is too small. To change the behaviour you can install your own handler with _set_invalid_parameter_handler[^].
 
Share this answer
 
v2
Simply provide a bigger buffer. If you really want to write an error the check the length of the string and use a buffer slight bigger that want you really want to allows.

For example, if the buffer size is 11, then if the length of the string is 10, then you know that the formated string is too big to fit maximum desired size.

On the other hand, if there are no limit on string size, then you might consider using C++ code to create the string (like a string stream)or a relatively large buffer so that it would always be big enough to handled valid case.

Typically in a case like the one above you probably should have an idea of the maximum length of each part... thus you be able to figure out required buffer size so that an error would never occurs because the buffer would be big enough for the worst case.
 
Share this answer
 
solution below,


const char* chText[33] = "Version %d.%02d [MemberId :- %s]";
char l_Title_ca[10];
try{
sprintf_s(l_Title_ca,sizeof(chText),chText,Ver,Rel,MemID);
}
catch(char* str)
{
cout << "error: ";
// if(errn == BUFFER_TOO_SMALL)
{
cout << "storage buffer too small.\n";
return 0;
}
}catch(...)
{
cout<<"caught..."<<endl;
}


currently i am not getting any error message , which is like "buffer too small"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900