# Exceptions in initialize()?

**URL:** <https://rubytalk.org/t/exceptions-in-initialize/6059>\
**Category:** ruby-talk\
**Created:** [20 May 2003 06:30 UTC](https://rubytalk.org/t/exceptions-in-initialize/6059 "2003-05-20T06:30:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jack\_Twilley](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@Jack\_Twilley](https://rubytalk.org/u/Jack_Twilley)\
**Post date:** [20 May 2003 06:30 UTC](https://rubytalk.org/t/exceptions-in-initialize/6059/1 "2003-05-20T06:30:58Z")

</div>

I’m very new to ruby, so please be gentle.

I have created a class and an initialize() method. I would like an  
attempt to initialize() to fail if either an argument is not supplied  
or the supplied argument does not match a regular exponent. The  
solution would appear to be to use an exception, but I cannot tell

- From the documentation what happens if an exception is thrown from  
within an initialize() method and not caught.

I know I can “just try it”, but I’m interested in knowing what’s  
supposed to happen in that case. I’m hoping that what is supposed to  
happen is that the new object is equal to nil, but I can’t be sure of  
the defined behavior until it’s defined.

Thank you for your patience and assistance.

Jack.

* * *

Jack Twilley  
jmt at twilley dot org  
http colon slash slash www dot twilley dot org slash tilde jmt slash

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [20 May 2003 07:05 UTC](https://rubytalk.org/t/exceptions-in-initialize/6059/2 "2003-05-20T07:05:14Z")

</div>

> I’m very new to ruby, so please be gentle.
> 
> I have created a class and an initialize() method. I would like an  
> attempt to initialize() to fail if either an argument is not supplied  
> or the supplied argument does not match a regular exponent. The  
> solution would appear to be to use an exception, but I cannot tell
> 
> - From the documentation what happens if an exception is thrown from  
> within an initialize() method and not caught.
> 
> I know I can “just try it”, but I’m interested in knowing what’s  
> supposed to happen in that case. I’m hoping that what is supposed to  
> happen is that the new object is equal to nil, but I can’t be sure of  
> the defined behavior until it’s defined.

> > class Foo; def initialize; raise “foo”; end; end  
> > =\> nil  
> > a = Foo.new rescue nil  
> > =\> nil  
> > a  
> > =\> nil

or

> > def Foo.new(\*args)  
> > begin  
> > ?\> super(\*args)  
> > rescue  
> > nil  
> > end  
> > end  
> > =\> nil  
> > a = Foo.new  
> > =\> nil

> **···**
>
> On Tue, May 20, 2003 at 03:30:58PM +0900, Jack Twilley wrote:
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> There are 3 kinds of people: those who can count & those who can’t.  
> – Unknown source

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [20 May 2003 07:31 UTC](https://rubytalk.org/t/exceptions-in-initialize/6059/3 "2003-05-20T07:31:29Z")

</div>

“Jack Twilley” [jmt+usenet@twilley.org](mailto:jmt+usenet@twilley.org) schrieb im Newsbeitrag  
[news:86u1bqi18y.fsf@duchess.twilley.org](mailto:news:86u1bqi18y.fsf@duchess.twilley.org)…  
-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

I’m very new to ruby, so please be gentle.

I have created a class and an initialize() method. I would like an  
attempt to initialize() to fail if either an argument is not supplied  
or the supplied argument does not match a regular exponent. The  
solution would appear to be to use an exception, but I cannot tell

- From the documentation what happens if an exception is thrown from  
within an initialize() method and not caught.

I know I can “just try it”, but I’m interested in knowing what’s  
supposed to happen in that case. I’m hoping that what is supposed to  
happen is that the new object is equal to nil, but I can’t be sure of  
the defined behavior until it’s defined.

```
Yes, it's nil because the assignment didn't take place. But typically

```

this is not too interesting since the code following the assignment will  
not be executed anyway:

irb(main):001:0\> class Foo  
irb(main):002:1\> def initialize(a)  
irb(main):003:2\> raise “should’ve been ‘a’” unless “a” == a  
irb(main):004:2\> @a=a  
irb(main):005:2\> end  
irb(main):006:1\> end  
nil  
irb(main):007:0\>  
irb(main):008:0\* f1 = Foo.new “a”  
#\<Foo:0x2aac998 @a=“a”\>  
irb(main):009:0\>  
irb(main):010:0\* Thread.new do  
irb(main):011:1\* f2 = Foo.new "b"  
irb(main):012:1\> puts "never here"  
irb(main):013:1\> end  
#\<Thread:0x2a51e08 dead\>  
irb(main):014:0\>  
irb(main):015:0\* p defined? f3  
nil  
nil  
irb(main):016:0\>  
irb(main):017:0\* begin  
irb(main):018:1\* f3 = Foo.new "b"  
irb(main):019:1\> puts "never here"  
irb(main):020:1\> rescue =\> e  
irb(main):021:1\> p e  
irb(main):022:1\> end  
#\<RuntimeError: should’ve been ‘a’\>  
nil  
irb(main):023:0\>  
irb(main):024:0\* p defined? f3  
"local-variable"  
nil  
irb(main):025:0\>

```
robert
```
