• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception in java  RSS feed

 
vineet chaturvedi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whenever we use try/catch block how we used to know that what type of exception is going to happen ???
Suppose here is a program a code is defined under the try block...
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.element At(i));

what exception is going to happen can someone predict that ??

or he/she has to learn every type of exception that can occur in any form ???
 
Nam Ha Minh
Ranch Hand
Posts: 515
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you have to refer to Javadocs/signature (throws clause) of the methods to find out what exceptions might occur.
Looks like you made a duplicate here:
http://www.coderanch.com/t/612902/java/java/explain-mechanism-block-statement
 
vineet chaturvedi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
throws clause can explain and predict what type of exception is going to happen ???

i think no.. we have to declare the exception in throws statement.
for example
throws <Exception type 1>,<Exception type 2>.....................<Exception type n>

yes i made a duplicate because need to understand the concept behind this
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vineet chaturvedi wrote:throws clause can explain and predict what type of exception is going to happen ???


Yes. In fact it is required to do so.

i think no.. we have to declare the exception in throws statement.


For each method that you call, you look at its javadocs (or its throws clause, if you have access to the source) to see what checked exceptions it can throw. Then for each one of those exceptions, your method must catch it or declare it in its own throws clause. And of course any checked exceptions that you throw explicitly must be declared as well.
 
vineet chaturvedi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks very much.......
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!