• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Exception Handling in Main

 
Greenhorn
Posts: 8
  • 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
 
Bartender
Posts: 4100
63
Hibernate Fedora Chrome
  • 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.
 
Ranch Hand
Posts: 35
  • 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
 
Marshal
Posts: 64705
225
  • 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
  • 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
Marshal
Posts: 64705
225
  • 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.
 
Rancher
Posts: 3742
16
  • 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.
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • 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
Marshal
Posts: 64705
225
  • 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
  • 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
Marshal
Posts: 64705
225
  • 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
  • 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: 2856
10
Firefox Browser Fedora Java
  • 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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!