This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes classes with constructors throwing exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "classes with constructors throwing exceptions" Watch "classes with constructors throwing exceptions" New topic
Author

classes with constructors throwing exceptions

kevinn lee
Ranch Hand

Joined: Feb 15, 2010
Posts: 87
"instantiating of classes having constructors that throw exceptions is not possible"
as far as I know this is correct because the procedure of creating the object wont complete because of the exception.Am I correct?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

1. Where did you read that statement
2. If that statement is talking about constructors that have some exceptions in their throws clause, then the statement is wrong. You can create instances of such classes, you just have to wrap the instantiation in a try-catch block...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
kevinn lee
Ranch Hand

Joined: Feb 15, 2010
Posts: 87
Ankit Garg wrote:1. Where did you read that statement
2. If that statement is talking about constructors that have some exceptions in their throws clause, then the statement is wrong. You can create instances of such classes, you just have to wrap the instantiation in a try-catch block...


you mean like the following



Since the compiler says that variable q is unknown(line 1) I thought the instantiation process has not completed.Am I wrong?
thanks
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

This is because you have declared q inside the try block. It is not accessible outside the try block. Please declare "q" outside "try" as in code below.
Small request . Please write more readable code if you can


SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
kevinn lee
Ranch Hand

Joined: Feb 15, 2010
Posts: 87
Rajeev Rnair wrote:This is because you have declared q inside the try block. It is not accessible outside the try block. Please declare "q" outside "try" as in code below.
Small request . Please write more readable code if you can



sorry about the untidy code.
but even here I cant see object initialization taking place.Output is "null".How should the code change in order to make sure that a object is initialized?
thanks
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hi,

How do you expect that object will be created and initialized when you are throwing
exception from within the constructor. It will never complete. Try omitting the line which
is again throwing the exception within the constructor.

Hope this helps,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
kevinn lee
Ranch Hand

Joined: Feb 15, 2010
Posts: 87
Prithvi Sehgal wrote:Hi,

How do you expect that object will be created and initialized when you are throwing
exception from within the constructor. It will never complete. Try omitting the line which
is again throwing the exception within the constructor.

Hope this helps,

hello

the statement is correct then?
thanks prithvi
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Kevin,

As Ankit has quoted it is applicable to the throws clause. For example



Case 1- Object initialization can be done, you just need to wrap the instance inside try-catch.
Case 2- Can't happen as you are explicitly throwing an exception.

Hope this helps,
kevinn lee
Ranch Hand

Joined: Feb 15, 2010
Posts: 87
thanks
now I got Ankits point.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
You are welcome mate.

Good luck for SCJP Journey,
 
GeeCON Prague 2014
 
subject: classes with constructors throwing exceptions