File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Handling in Main

 
Jalal Albasri
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm writing a program in which the main method calls a method in another class to do all data handling. The method being called may throw NullPointer and FileNotFound exceptions. Do I need to put parts of the data handling method in try catch statements or can i just put the try catches around the call to it in the main method?

I guess what I'm asking is, can i just put my try catches all in the main method?
THANKS
 
Vijitha Kumara
Bartender
Posts: 3908
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess what I'm asking is, can i just put my try catches all in the main method?


You should, if the method being called throws any checked exceptions. Otherwise compiler will complain.
 
Adinath Shirsath
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you can do it
you have to just add throws clause in your method declaration which you are calling from your main method
 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disagree with you both.

You need to consider what you are going to do if those Exceptions occur; are you going to try to find the file again? If so, let the method which might suffer the Exception declare it, and put the handling in the method which called that method. A while loop with "try again" in, maybe. If you put all your Exception handling in the main (not "Main") method it will become unmanageably large, and if you declare the Exceptions from the main method that will pass the handling over to the JVM which follows its default behaviour: print a stack trace and exit the thread with the Exception in.

Exception handling is a complicated subject; a search of JavaRanch will reveal lots of similar topics. Also there is something in the Java Tutorials.
 
Jalal Albasri
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys.. I'm gonna put them in the main method in that case.. just cos its neater its a small prog and there isn't much handling to be done..

..Do i also have to put the throws clause in the main method declaration?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jalal Albasri:
Do i also have to put the throws clause in the main method declaration?


No. You have to declare a checked Exception with a throws or handle it with try-catch try-catch-finally, but not both.

Don't know whether you can use try-finally without catch.
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Don't know whether you can use try-finally without catch.


You can.
 
Amit Ghorpade
Bartender
Posts: 2851
10
Fedora Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jalal Albasri:
Thanks guys.. I'm gonna put them in the main method in that case.. just cos its neater its a small prog and there isn't much handling to be done..

..Do i also have to put the throws clause in the main method declaration?


Please avoid use of non real words in your post."cos" and "prog" are not real words.Read this for more information.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Joanne and Amit. I didn't notice the "cos."
 
Jalal Albasri
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, sorry about the "prog".

Alright I think i've got the exceptions sussed for the most part. I handle in the method with a try catch and propagate another. They are both IOExceptions.

I read that if it is a checked exception it must be handled in some way. What about if it is an unchecked or runtime exception (are they the same thing?) like a NullPointerException, can I use a try catch or should they only be used for checked exceptions?

Thanks for all the help btw.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is discussion about checked and unchecked Exceptions in the Java Tutorials link I sent earlier. There is a section called something like checked and unchecked exceptions the controversy; read that.
Subclasses of java.lang.Error and java.lang.RuntimeException are unchecked; they should mostly not be tried and caught and if declared they should be declared in the javadoc comment only. There are a few unchecked Exceptions (eg NumberFormatException), however. which might be worth catching.
 
Jalal Albasri
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is discussion about checked and unchecked Exceptions in the Java Tutorials link I sent earlier. There is a section called something like checked and unchecked exceptions the controversy; read that.
Subclasses of java.lang.Error and java.lang.RuntimeException are unchecked; they should mostly not be tried and caught and if declared they should be declared in the javadoc comment only. There are a few unchecked Exceptions (eg NumberFormatException), however. which might be worth catching.


Thanks so much that cleared it up!
 
Amit Ghorpade
Bartender
Posts: 2851
10
Fedora Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Thank you, Joanne and Amit. I didn't notice the "cos."


You are welcome Campbell
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic