• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

classes with constructors throwing exceptions

 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 310
1
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
now I got Ankits point.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome mate.

Good luck for SCJP Journey,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic