• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Question on exception handling

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider if a method throws a checked exception and it is enclosed inside another method which is called from main function.

so now should all the functions either handle or declare the exception.

pls explain...
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you mean this call stack:

main() --> method1() --> method2() throws Exception

* Yes, method1 and main may declare that Exception is thrown and let JVM handle the Exception.

* Or you could catch and handle it in method1

* Or you could make method1() throw the Exception to main() and use a try catch block in main() to handle it. If you dont want to catch it then i guess you have to go for the first option.
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any method which contains a line or method call which can throw a checked exception has to declare the exception in a throws clause or handle the exception.
 
Lavanya Raguram
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now if method2 handles the exception by a try/catch block then should method1 or main or both declare or handle the exception ?
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if method 2 try/cathes the exception we need not do anything else for the exception in main as it had been caught.

main method has to work for the exception if method2 only declares it and not handles.when we say declare we are annoucing the exception and not catching it.
 
Lavanya Raguram
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
still i am confused :roll:

consider if it is main=>method1=>method2=>method3(throws exception)

now consider method3 handles it using try\catch block so should method2 declare or handle it.
also should method1 declare or handle it.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I would say. Method 2 and method 1 should declare or handle it, otherwise you get a compile time error.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
main=>method1=>method2=>method3(throws exception)

suppose if we caught the exception in method3 and we do not handle or declare it in method2 and 1, we still get a compile time error..
This is because method 2 contain the code which causes the exception ,hence we have to declare or handle in method 2...
if we declare in method2 then we have to caught or declare it in method 1.and it continues upto main...

suppose if we caught in method2 there is no need of declare or caught in method 1 or main...

---------------------------------------------------------------------------- Magesh.s
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is simple actually:

If any method, no matter how deep in the stack trace hierachy, can throw a checked exception, you have two options, whether....

1) your method handles the execption itself by means of a try/catch clause, in whose case, any call to this method will not require further exception handling for this particular exception. That's to say, other methods can call your the "exception-handled" method without worrying about the exceptions happening inside of it.

or

2) you decide not to handle the exception and rethrow it with the throws clause in the method signature, in whose case, you imply another method will have to do something with the exception, wheter it is handling or rethrowing it again, up the stak trace. That's to say, any other methods calling you method will have to do something about the exception.

Of course, this is just for checked exceptions.

That been said, there could be many combinations:

method1()->method2()->method3()(throws Exception)

Then you could:

1. throws<-throws<-throws
2. nothing<-nothing<-handle
3. nothing<-handle<-throws
4. handle<-throws<-throws

I drew the arrows in the inverse direction, because exceptions travel upwards in the stack trace.

The fact is, when you throw the exception up the stacktrace the receivin method has to do somethig. If you handle it, it does not.

[ April 18, 2006: Message edited by: Edwin Dalorzo ]
[ April 18, 2006: Message edited by: Edwin Dalorzo ]
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider if it is main=>method1=>method2=>method3(throws exception)
Lavanya,

If exception was caught using try catch block, as exception is not propagating it is not necessary to declare "throws" in main,method1,method2 .
I the exception is not caught ,then it is necessary to write throws class. One thing you have to remember if exception is propagating then it is required to write throws like below

main() throws IOException
{
method1();
}
method1() throws IOException
{
method2();
}
method2()throws IOException
{
method3();
}
method3()throws IOException()
{
throw new IOException();
}
if it is caught anywhere in between then no need to write throws clause...

like below
main()
{
method1();
}
method1()
{
try
{
method2();
}
catch(IOException e)
{
}
}
method2()throws IOException
{
method3();
}
method3()throws IOException()
{
throw new IOException();
}

I hope it is clear
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wanted to add one note. There is no need to write the try catch for each caller method. If the exception is handled in top most caller, it serves the purpose.
e.g.

class t
{
public static void main(String[] args) {
t tt= new t();
try{
tt.change (a) ;
}catch(Exception ae){}
}
void change(Integer a)throws Exception
{
aaa();
}
void aaa() throws Exception
{
}
}
 
Please enjoy this holographic presentation of our apocalyptic dilemma right after this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic