Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

classes with constructors throwing exceptions

 
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?
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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
 
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • 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
 
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,
 
Do you pee on your compost? Does this tiny ad?
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic