permaculture playing cards*
The moose likes Beginning Java and the fly likes Exception Handling in Main Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception Handling in Main" Watch "Exception Handling in Main" New topic
Author

Exception Handling in Main

Jalal Albasri
Greenhorn

Joined: May 17, 2008
Posts: 8
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

Joined: Mar 24, 2008
Posts: 3817

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Adinath Shirsath
Ranch Hand

Joined: Dec 04, 2007
Posts: 35
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

Joined: Oct 13, 2005
Posts: 38057
    
  22
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

Joined: May 17, 2008
Posts: 8
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

Joined: Oct 13, 2005
Posts: 38057
    
  22
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

Joined: Aug 05, 2005
Posts: 3432
    
  12
Originally posted by Campbell Ritchie:
Don't know whether you can use try-finally without catch.


You can.


Joanne
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

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.


SCJP, SCWCD.
|Asking Good Questions|
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
Thank you, Joanne and Amit. I didn't notice the "cos."
Jalal Albasri
Greenhorn

Joined: May 17, 2008
Posts: 8
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

Joined: Oct 13, 2005
Posts: 38057
    
  22
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

Joined: May 17, 2008
Posts: 8
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

Joined: Jun 06, 2007
Posts: 2716
    
    6

Originally posted by Campbell Ritchie:
Thank you, Joanne and Amit. I didn't notice the "cos."


You are welcome Campbell
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Handling in Main
 
Similar Threads
Unreported exception
try catch error handling
Simple For Loop Question
Universal way to detect field changes?
Exceptions