• Post Reply Bookmark Topic Watch Topic
  • New Topic

please explain the mechanism of this try block statement  RSS feed

 
vineet chaturvedi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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 ???
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
here is the full code ...
 
vineet chaturvedi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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 ???
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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 ???

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • 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.
 
James X Peterson
Whizlabs Java Support
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!