File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception" Watch "Exception" New topic
Author

Exception

Madan Mohan
Ranch Hand

Joined: Dec 05, 2008
Posts: 30
hello Genius,

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




Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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);


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
santosh pilla
Greenhorn

Joined: Jan 14, 2009
Posts: 9
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
{}
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Please Quote your source


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Exception
 
Similar Threads
Exception Handing
Exception Handing
K&S chapter 3 q-6
Exception Handing
exception question from kb