Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #118829
| From | Zhengyi Fu <i@fuzy.me> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Q: how to make an object that throws |
| References | <n6Ocnf_tkL8shbr7nZ2dnZfqn_qdnZ2d@giganews.com> |
| Date | 2024-04-23 11:08 +0800 |
| Message-ID | <87wmoon5d3.fsf@fuzy.me> (permalink) |
Ross Finlayson <ross.a.finlayson@gmail.com> writes:
> Hello I hope I might consult your expertise, I'm wondering about
> a use-case where for a given library object, to provide an instance
> of same type, that on any access, throws an exception.
>
> The idea is that there's an idea in a different language where
> the runtime throws a null_pointer_dereference exception
> when dereferencing a null pointer, yet in C++ it's signal segfault.
>
> So, what I wonder about is how to make an object in C++,
> of a given standard library type, and any arbitrary type,
> that on any access, throws.
>
> I suppose I might static_cast an object yet I wonder how
> to make it so then that any access to it throws.
>
> As you might imagine it's a use-case for exceptions
> as part of flow-of-control, that in the other language
> just is null yet in C++ would be for "an object of all types",
> that throws on any access.
>
I believe there is no portable way to implement this.
However, if you are running GCC on supported platforms, you can try
the `-fnon-call-exceptions` option.
--8<---------------cut here---------------start------------->8---
// main.cpp
#include <sys/types.h>
#include <signal.h>
#include <iostream>
#include <exception>
extern "C" void handle_signal(int signum) {
throw std::runtime_error("Invalid access");
}
int main()
{
struct sigaction act;
act.sa_handler = &handle_signal;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_NODEFER;
sigaction(SIGSEGV, &act, nullptr);
int *object = NULL;
try {
// write
*object = 100;
} catch (std::exception& exc) {
std::cerr << "Caught exception: " << exc.what() << '\n';
}
try {
// read
int value = *object;
std::cout << "value = " << value << '\n';
} catch (std::exception& exc) {
std::cerr << "Caught exception: " << exc.what() << '\n';
}
}
--8<---------------cut here---------------end--------------->8---
Compile the source file with
g++ -fnon-call-exceptions -o test main.cpp
Run ./test, it outputs
Caught exception: Invalid access
Caught exception: Invalid access
--
Zhengyi Fu
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Q: how to make an object that throws Ross Finlayson <ross.a.finlayson@gmail.com> - 2024-04-22 19:11 -0700
Re: Q: how to make an object that throws Sam <sam@email-scan.com> - 2024-04-22 22:32 -0400
Re: Q: how to make an object that throws Ross Finlayson <ross.a.finlayson@gmail.com> - 2024-04-22 21:51 -0700
Re: Q: how to make an object that throws Sam <sam@email-scan.com> - 2024-04-23 07:26 -0400
Re: Q: how to make an object that throws Ross Finlayson <ross.a.finlayson@gmail.com> - 2024-04-23 08:39 -0700
Re: Q: how to make an object that throws Sam <sam@email-scan.com> - 2024-04-23 22:08 -0400
Re: Q: how to make an object that throws Ross Finlayson <ross.a.finlayson@gmail.com> - 2024-04-23 20:07 -0700
Re: Q: how to make an object that throws Ross Finlayson <ross.a.finlayson@gmail.com> - 2024-04-23 22:10 -0700
Re: Q: how to make an object that throws Zhengyi Fu <i@fuzy.me> - 2024-04-23 11:08 +0800
Re: Q: how to make an object that throws Ross Finlayson <ross.a.finlayson@gmail.com> - 2024-04-22 21:52 -0700
Re: Q: how to make an object that throws wij <wyniijj5@gmail.com> - 2024-04-25 02:56 +0800
Re: Q: how to make an object that throws Ross Finlayson <ross.a.finlayson@gmail.com> - 2024-04-24 12:02 -0700
Re: Q: how to make an object that throws Ross Finlayson <ross.a.finlayson@gmail.com> - 2024-04-24 13:56 -0700
Re: Q: how to make an object that throws Ross Finlayson <ross.a.finlayson@gmail.com> - 2024-04-27 08:09 -0700
Re: Q: how to make an object that throws Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-04-25 14:08 -0700
csiph-web