• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Experimentation: Exception Stack Trace Implementation

 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I have the following code which is suppose to be an implementation of a resource handle



What I would like to know is how can I pinpoint the file-name and line number when an exception is thrown...

If I had the following code in a file cust.cpp:


What code must I write so that I can state the following -> NullPointerException: at cust.cpp:2
 
Ranch Hand
Posts: 69
2
Netbeans IDE C++ Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The standard defines two macros "__FILE__" and "__LINE__".

With a bit of macro-magic, these can be easily inlined into a throw statement, although you would have a bit of trouble enforcing its use. However, you should ask yourself the question: Should I be doing this in the beautiful [b]modern C++[/b] code that you've written?

[code=C++]
#include <string>
#include <iostream>

struct custom_exception
{
const char* what;
const char* file;
int line;

custom_exception(const char* what, const char* file, int line) :
what(what), file(file), line(line)
{
}

};
#define THROW(x) throw(custom_exception(x, __FILE__, __LINE__));

int main()
{
try
{
THROW("test");
}
catch(const custom_exception& ce)
{
std::cout << "Exception @ " << ce.file << ":" << ce.line << ":" << ce.what << std::endl;
}
}
[/code]
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks... I have an idea
 
We can walk to school together. And we can both read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic