Prevent class instantiation from the initialize method?

D

Dave Mihalik

Can some show me how to implement this java program using Ruby?
I am simply trying to prevent the testclass from being instantiated in
the constructor.
I am then checking to see if the the class has instanatiated properly.

class testclass
{
public testclass() throws Exception
{

Exception testexception;
testexception = new Exception("Instantiation cancelled.");
throw testexception;

}}

public class testprogram
{
public static void main(final String taArgv[])
{
testclass mytest = null;

try
{
mytest = new testclass();
}
catch(Exception myexception)
{
System.out.println(myexception.getMessage());
}

if (mytest == null)
{
System.exit(1);

}
System.out.println("Instantiation not cancelled.");
}}


I am new to Ruby but this is what I have so far:

class Testclass
def initialize()

raise "Instantiation cancelled."

end
end

begin

mytest = Testclass.new()

rescue Exception => e
begin

puts e
exit 1

end

puts "Instantiation not cancelled."


end

If the raise line in the initialize method is disabled, the puts
"Instantiation not cancelled." line does not execute.
 
J

Jake McArthur

Can some show me how to implement this java program using Ruby?
I am simply trying to prevent the testclass from being instantiated in
the constructor.
I am then checking to see if the the class has instanatiated properly.

This seems like a bad idea. I can think of no good reason to do this.
This is probably just a bad solution to a very different problem.
What exactly are you planning to do with this?

- Jake McArthur
 
M

Marcin Mielżyński

Dave Mihalik wrote:

class Testclass
def initialize()

raise "Instantiation cancelled."

end
end


begin
mytest = Testclass.new()
rescue Exception => e

puts e
exit 1

end

puts "Instantiation not cancelled."


rescue/ensure must have it's own begin clause

lopex
 
M

Matthew Smillie

Can some show me how to implement this java program using Ruby?
I am simply trying to prevent the testclass from being instantiated in
the constructor.
I am then checking to see if the the class has instanatiated properly.

[Java.elide()]

I am new to Ruby but this is what I have so far:

class Testclass
def initialize()

raise "Instantiation cancelled."

end
end

begin

mytest = Testclass.new()

rescue Exception => e
begin

puts e
exit 1

end

puts "Instantiation not cancelled."


end

If the raise line in the initialize method is disabled, the puts
"Instantiation not cancelled." line does not execute.

Because it's always going to execute that inner begin...end block. I
expect you want something more like the following:

begin
mytest = Testclass.new
rescue Exception => e
puts e
exit 1
end
puts "Instantiation not cancelled"

The first 'begin' starts the protected block, similar to Java's try
{ ... }, and the 'rescue' acts on any exceptions raised within that
block.

That said, using exceptions for control flow is usually a bit of
pathological case in Ruby, and there's usually a better way to
accomplish the same result. The two possible answers that spring to
mind are (simple) making the initialiser private or (slightly more
complicated) just removing the new method all together:

class Testclass
class << self
undef_method :new
end
end

my_test = Testclass.new()
NoMethodError: undefined method `new' for Foo:Class
from (irb):6
from :0



matthew smillie.
 
D

Dave Mihalik

That said, using exceptions for control flow is usually a bit of
pathological case in Ruby, and there's usually a better way to
accomplish the same result. The two possible answers that spring to
mind are (simple) making the initialiser private or (slightly more
complicated) just removing the new method all together:


I am trying to determine how my program should handle errors in the
initialize method. If an error occurs in code that is running in the
initialize method, I want to be able to prevent the class from
instantiating.


class Testclass
def initialize()

## Do work here.
## Do more work here.
## Get an error from doing more work.

## Raise an error to prevent the class from instantiating because
## the functionality of the class depends on the above work.

##raise "Instantiation cancelled."

end
end

## Try to instantiate the class.
begin

mytest = Testclass.new()

## Make sure that the instantiation was successful.
rescue Exception => e

## If not, handle the error.
puts e
exit 1

end

## If instantiation succeeded, then proceed with the program.
puts "Instantiation not cancelled."

Is there a better way to handle errors in the initialize method?
 
A

Alex Young

Dave Mihalik wrote:
I am trying to determine how my program should handle errors in the
initialize method. If an error occurs in code that is running in the
initialize method, I want to be able to prevent the class from
instantiating.
<snip pseudocode>

It depends on the nature of the code that can fail, and what *exactly*
you mean by "instantiating".

This might work, if the failable code is relevant outside an instance:

class TestClass
def TestClass.get_new(params)
begin
settings = do_work_that_can_fail(params)
new(settings)
rescue
puts "Waaah! It didn't work!"
nil
end
end
end

If that's not appropriate, then there's this:

class TestClass
def TestClass.get_new(params)
begin
new(params)
rescue
puts "Still didn't work!"
nil
end
end
def initialize(params)
do_work_that_can_fail(params)
end
end

In the second case (unless I'm mis-remembering how the object lifecycle
works) the instance is created in the new() call, but it'll get disposed
of in the next GC round because there aren't any references to it being
held anywhere.

In both cases, you just call TestClass.get_new() rather than
TestClass.new() to get your instance, or nil if the stuff that can fail
fails.
 
D

Dave Mihalik

Jake said:
This seems like a bad idea. I can think of no good reason to do this.
This is probably just a bad solution to a very different problem.
What exactly are you planning to do with this?

- Jake McArthur

I am trying to handle any errors that may occur in the initialize
method. I don't want to the class to instantiate if errors occur in the
initialize method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top