Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #118871

Re: Q: how to make an object that throws

From wij <wyniijj5@gmail.com>
Newsgroups comp.lang.c++
Subject Re: Q: how to make an object that throws
Date 2024-04-25 02:56 +0800
Organization A noiseless patient Spider
Message-ID <d9a8168224d6e2ed50cc57e25ca044139ff6f3da.camel@gmail.com> (permalink)
References <n6Ocnf_tkL8shbr7nZ2dnZfqn_qdnZ2d@giganews.com> <87wmoon5d3.fsf@fuzy.me>

Show all headers | View raw


On Tue, 2024-04-23 at 11:08 +0800, Zhengyi Fu wrote:
> 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
> 

Convert to program using libwy C++ library:
(Thanks, learned something.)

//--------- t.cpp
#include <Wy.stdio.h>
#include <Wy.signal.h>

using namespace Wy;

static void handle_signal(int signum) {
 cerr << "handle_signal(" << signum << ")" WY_ENDL;
 WY_THROW( Errno(EFAULT) );
}

int main() {
 Errno r;

 if((r=sigaction(SIGSEGV, SigAct(handle_signal,SigSet(),SA_NODEFER)))!=Ok) {
   WY_THROW(r);
 }

  int *object = NULL;

  try {
    // write
    *object = 100;
  }
  catch (Errno& e) {
    cerr << "Caught exception: " << wrd(e) << WY_ENDL;
  }

  try {
    // read
    int value = *object;
    cout << "value = "  << value << WY_ENDL;
  }
  catch (Errno& e) {
    cerr << "Caught exception: " << wrd(e) << WY_ENDL;
  }

};

//------------------------------
[]$ g++ -fnon-call-exceptions t.cpp -lwy
[]$ ./a.out
handle_signal(11)
Caught exception: Bad address, t.cpp(53)
handle_signal(11)
Caught exception: Bad address, t.cpp(53)


Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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