How to get the correct exception file/line.


 
Thread Tools Search this Thread
Top Forums Programming How to get the correct exception file/line.
# 1  
Old 07-09-2009
How to get the correct exception file/line.

The below code throws error in the line number 32 where the function is defined.

But How to find the line where the function is called.
That is I want to throw the error at the line number 43 (as here the function is called).


The code is:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class myEx {
std:: string _file, _message;
int _line;
public:
myEx(std::string file, int line);
std::string toString();
};
myEx::myEx(string file, int line) {
_file = file;
_line = line;
_message = "Unable to copy. ";
toString();
}
string myEx::toString()
{
ostringstream os;
os << _message <<"Error occurred at " << _file << ":" << _line;
return os.str();
}
template <typename T, int size_x>
static void toCopy(T(&x)[size_x], const T(*y)) {
if(strlen(y) >= size_x) {
throw myEx(__FILE__, __LINE__);
}
strncpy(x, y, size_x);
}

int main()
{
char first[4];
string second = "WELCOME";
try {
toCopy(first, second.c_str());
cout << "Copied Successfully! the " <<first<<endl;
}
catch(myEx &e)
{
cout << e.toString() << endl;
}
return 0;
}

---------- Post updated at 10:34 PM ---------- Previous update was at 10:32 PM ----------

Using macros works well. But I dont want to use macros.
# 2  
Old 07-09-2009
please format the code with the CODE tags to make your code easily readable. Also, run cat -n or something to number the lines if possible.

Include the error message that your getting during the build. Is it a run time error or compile error?
# 3  
Old 07-09-2009
If you compile the code with -ggdb and run it with gdb, it may be able to give you a backtrace(the command for which inside gdb is bt), which would list all the function calls on the stack.
# 4  
Old 07-10-2009
Thanks for the reply.
Below I copy the same code with the line numbers.
The File.C is:
Code:
     1  #include <iostream>
     2  #include <string>
     3  #include <sstream>
     4
     5  using namespace std;
     6
     7  class myEx {
     8    std:: string _file, _message;
     9    int _line;
    10  public:
    11    myEx(std::string file, int line);
    12    std::string toString();
    13  };
    14
    15  myEx::myEx(string file, int line) {
    16    _file = file;
    17    _line = line;
    18    _message = "Unable to copy. ";
    19    toString();
    20  }
    21
    22  string myEx::toString()
    23  {
    24    ostringstream os;
    25    os << _message <<"Error occurred at " << _file << ":" << _line;
    26    return os.str();
    27  }
    28
    29  template <typename T, int size_x>
    30  static void toCopy(T(&x)[size_x], const T(*y)) {
    31    if(strlen(y) >= size_x) {
    32      throw myEx(__FILE__, __LINE__);
    33    }
    34    strncpy(x, y, size_x);
    35  }
    36
    37  int main()
    38  {
    39    char first[4];
    40
    41    string second = "WELCOME";
    42    try {
    43      toCopy(first, second.c_str());
    44      cout << "Copied Successfully! the " <<first<<endl;
    45    }
    46    catch(myEx &e)
    47    {
    48      cout << e.toString() << endl;
    49    }
    50    return 0;
    51  }


The output is (as programmed):
Unable to copy. Error occurred at File.C:32

There is no compilation or run time error. The code behaves as programmed. As the array size is less i.e. 4 (“first [4]”) compared to the length of string “WELCOME” it throws error as expected.

The error is thrown with file name and line number. As programmed the error is thrown at the line number 32 (In the function definition).

I want to program (without using macros) such that the error should show the line number where the function is called. In our case it should show the line number 43. So that it would be easy to find which function call (if I call the function in different places) gives the problem.

Last edited by vgersh99; 07-14-2009 at 10:09 AM.. Reason: code tags, PLEASE!
# 5  
Old 07-11-2009
Since both __LINE__ and __FILE__ are macros, you're going to have a really hard time coming up with a way to do what you want without using any macros.
# 6  
Old 07-14-2009
achenle,

I can use __LINE__ and __FILE__ , I have a problem in getting the required fine and line. As mentioned above I have to throw the file/line of the function call.

I have mentioned "without using macro" in the sense I should not define a macro function.
That is I should not do some thing like
#define toCopy
.....
...
.
# 7  
Old 07-14-2009
Quote:
Originally Posted by SamRoj
achenle,

I can use __LINE__ and __FILE__ , I have a problem in getting the required fine and line. As mentioned above I have to throw the file/line of the function call.

I have mentioned "without using macro" in the sense I should not define a macro function.
That is I should not do some thing like
#define toCopy
.....
...
.
You want values from __FILE__ and __LINE__. Since those are macros evaluated at compile time, you also need something evaluated at compile time.

You have two choices:

1. Explicitly write code so that you use __FILE__ and __LINE__ each and every time you need them, or

2. Write and use a macro.

There's a reason why macros exist: sometimes they're the only way to do what needs to be done.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

After Ftp'ing file to destination how to check the file if it is in correct ASCII and not corrupted

Hi Folks, While transferring file from FTP software like Filezilla the files gets corrupted. Is there any way I can check if the recently transferred file is in ASCII and not corrupted. I have tried using file -i filename command which does tell if the file character set is ASCII or binary... (6 Replies)
Discussion started by: Khan28
6 Replies

2. Shell Programming and Scripting

Automatically correct a File Name

Hi All Need a help to understand how one can automatically correct a file name using a shell script even if it is in a completely different format... Ex : adv_OK_0215_mem_rules_firing.txt / advex_0215_OK_me_rule_fire.txt (or in any other format as above) to :... (13 Replies)
Discussion started by: chatwithsaurav
13 Replies

3. Shell Programming and Scripting

Compare 2 files using third file as an exception

Hi All, Is it possible to compare 2 files using the contents of a third file as a guide using a bash script? I have 2 files I want t compare (1 and 2 below) the difference between the two is the first line (a) I want my script to ignore this difference if the difference is stored in file... (5 Replies)
Discussion started by: nwalsh88
5 Replies

4. Shell Programming and Scripting

Need correct pattern at end of line

I have some data as below. I need correct it as result. Data: BG1:100+10++II DG1:200+100+1234 DG2:300+200++II CG1:200+100+1111 DG2:400+100++II DG6:200+200+2345 DG2:400+100+2222 Result: BG1:100+10++II DG1:200+100+1234 DG2:300+200++1 CG1:200+100+1111 DG2:400+100++1... (4 Replies)
Discussion started by: mr.awk
4 Replies

5. Shell Programming and Scripting

Line numbers and exception to be caught in logs

Hi Folks, I have just basic queries is that suppose I have to monitor the logs then there is a command , suppose I have to monitor the abc.log which is updating dynamically within seconds so the command will be after going to that directory is .. tail -f abc.log Now please advise what about... (1 Reply)
Discussion started by: punpun66
1 Replies

6. Shell Programming and Scripting

Monitor logs for exception and if exception come then sent an email

Hi Folks, please advise , I have logs generated on unix machine at location /ops/opt/aaa/bvg.log , now sometimes there come exception in these logs also, so I want to write such a script such that it should continuously monitor these logs and whenever any exception comes that is it try to find... (3 Replies)
Discussion started by: tuntun27272727
3 Replies

7. Shell Programming and Scripting

Shell script to unmonitor the mounts in exception file

I am trying to write a shell script for monitoring the file system mount. command I am using will retrieve a output as shown below. /dev/fsv29 2% /apps/rj/pgl/bslSys I also need to add exception mounts in a file and I would like script to ignore the mount which I specify. If I add the... (6 Replies)
Discussion started by: chandu123
6 Replies

8. Shell Programming and Scripting

Catch a PL/SQL exception in ksh file

Hi all Im trying to call a PL SQl block from a ksh file like this : sqlplus -s $DB_USERID/$DB_PASSWD@$DB_NAME<<eof whenever SQLERROR exit 1 var varError VARCHAR2(200); exec ODAS_BATCH_JOBS_RETRIEVE.retrieve_user_info(:varError); eof If there is a error then varError will return a... (1 Reply)
Discussion started by: Sam123
1 Replies

9. UNIX for Dummies Questions & Answers

getting help on finding exception in running log file

Hi all, I am trying to write a script for an application server log file where i want to put this script as a cron tab entry and it will check the server log file last 1000/500 line for every fifteen minute. i am using the script like this. count=`tail -n 1000 Trace.log | grep -c... (1 Reply)
Discussion started by: senthilkumar_ak
1 Replies

10. UNIX Desktop Questions & Answers

file attributes and exception

hi, I want to know the date the file was created or modified. I can do this using ls, ll -ltr etc... I want to do this in a function (so If the file date is older then a week I can report it), is there a way? another thing... In sql function, I can catch exceptions, is there a way to do this... (1 Reply)
Discussion started by: krem
1 Replies
Login or Register to Ask a Question