• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello Genius,

Could anyone please explain me this code.. how the invocation is gonna happen...




 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the flow is like this

-->in main
-->new Plane().s1();
-->in s1
--> try{s2();}
-->in s2
-->s3();
-->in s3
-->throw new Exception();
-->back in s2, no catch block so again back to calling code
-->back in s1
-->catch(Exception e){s +="c";}
-->back in main
-->System.out.println(s);
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Madan Mohan wrote:

Could anyone please explain me this code.. how the invocation is gonna happen...



Well, It's starts from the method call s1(), which calls the method s2(), then again s2() calls s3() within itself. Since s3() throws an exception after #1 it jumps to the s1()'s catch block which adds "c" to the String variable s which contains the String "-" already resulting "-c". So will see the output "-c" if you try this.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)In the above code when s2() function is being called within s1 .then s2 function is like this
void s2() throws Exception

but here within s2() function we are not throwing any thing ...like throw new Exception ..so why doesn't the compiler sees it and consider it as a mistake !



2) and s2() function throws Exception like

void s2() throws Exception then the function which calls it (s1) should either handle the exception or it may pass the excepion but here it is handling it ..but s2() function doesnt have any keyword like throw ......

3)void s2() throws Exception means --- Exception class is a super class of all Exceptions (checked and unchecked ) then when we say throws Exception then is it throwing checked Exception or unchecked Exception ...



4) can we have try and catch block in void s3() function as

void s3() throws Exception
{
try
{}
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please Quote your source
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

santosh pilla wrote:1)In the above code when s2() function is being called within s1 .then s2 function is like this void s2() throws Exception but here within s2() function we are not throwing any thing ...like throw new Exception ..so why doesn't the compiler sees it and consider it as a mistake !


Why should the compiler consider it as a mistake. If you are thinking how try and catch blocks work, it is not the case with the keyword "throws"
In case of try if you are not throwing a Checked Exception, the compiler gives an error. But, if throws declares a Checked Exception, it doesn't bother to see if the exception is really thrown.
And in this case the code could actually throw an exception. It is calling s3 which throws an exception, so it either has to handle it or declare.

santosh pilla wrote:2) and s2() function throws Exception like void s2() throws Exception then the function which calls it (s1) should either handle the exception or it may pass the excepion but here it is handling it ..but s2() function doesnt have any keyword like throw ......


You are in java, please call it a method rather than a function.
s2() method doesn't have a throw keyword, but it has a call to s3 which could throw an exception

santosh pilla wrote:3)void s2() throws Exception means --- Exception class is a super class of all Exceptions (checked and unchecked ) then when we say throws Exception then is it throwing checked Exception or unchecked Exception ...


It means both. But the compiler worrys only about the Checked Exception
Suppose a method1() throws RuntimeException. You have another method2() calling method1(). Then method2() doesn't need to handle or declare it.
Suppose a method1() throws Exception. You have another method2() calling method1(). Then method2() does need to handle or declare it.

santosh pilla wrote:4) can we have try and catch block in void s3() function as
void s3() throws Exception
{
try
{}


No.You haven't caught any exceptions. And try catch need you to actually throw a checked Exception
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic