• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

please explain the mechanism of this try block statement

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try {
System.out.println("Entering" + " try statement");

out = new PrintWriter(new FileWriter("OutFile.txt"));
for (int i = 0; i < SIZE; i++)
out.println("Value at: " + i + " = " + vector.elementAt(i));

can someone explain me mechanism of this try block statement how is it dealing with exception ???
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code is incomplete. We'd need to see the rest of the code.
 
vineet chaturvedi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is the full code ...
 
vineet chaturvedi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much for this.....i will take care this in future...

can you please explain me the question please ???
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vineet chaturvedi wrote:can you please explain me the question please ???



You asked, "how is it dealing with exception?" What do you mean by that question? What don't you understand? Have you gone through a java exception tutorial yet?
 
vineet chaturvedi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay first of all tell me what type of code is this in the above program me

out = new PrintWriter(new FileWriter("OutFile.txt"))


is it a object ,constructor or what it is ???

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vineet chaturvedi wrote:okay first of all tell me what type of code is this in the above program me

out = new PrintWriter(new FileWriter("OutFile.txt"))


is it a object ,constructor or what it is ???


That's just creating a new object, and then using that as an argument to the constructor when creating a second new object. It's equivalent to this:
I assume you follow those two lines? The only difference is that in the original version we haven't kept a reference to the intermediate object, whereas in the second we could refer to afterwards using the writer variable. It's nothing to do with exceptions, though.
 
Whizlabs Java Support
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.When you get an exception in try block related exception object will be thrown from try block and it will be caught by the corresponding catch block. In your code if ArrayIndexOutOfBoundsException is raised then first catch block will be executed,if IOException is raised second catch block will be executed. finally is the block that will execute regardless of exception raised or not.

Regards,
James
 
This tiny ad is wafer thin:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic