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 Exceptions doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exceptions doubt" Watch "Exceptions doubt" New topic
Author

Exceptions doubt

Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi All,
Source: k&b

class MyException extends Exception{
void someMethod() throws MyException {
doStuff();
}
void doStuff() throws MyException {
try{
throw new MyException();
}
catch(MyException me) {
throw me;
}
}
}

I am not getting the below mentioned code. My doubt is "MyException" is thrown in try block and again "MyException" is thrown in catch block. So can somebody explain me how it works?.


void doStuff() throws MyException {
try{
throw new MyException();
}
catch(MyException me) {
throw me;
}
}



Thanks All
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Nik,



The catch block argument is ref variable of MyException. In the try block
you are throwing the MyException (you just created a new instance of MyException and threw it), it is caught by the catch block. What is caught by the catch block is again thrown by it.

Be careful, if your doStuff wont declare MyException, it was Compiler error to write "throw me" inside the catch block, and it would be called unhandled exception.

The exception thrown in the try block is caught by the catch block and the exception rethrown in the catch block is simply ducked to the method doStuff(). And whichever methods call doStuff(), they will have to place this method call statement either in the try catch of declare the exception.


cmbhatt
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi chandra,
I got it. I am not understanding the below mentioned code its from k&b.

import java.io.*;
class Master{
String doFileStuff() throws FileNotFoundException { return "a"; }
}

class Slave extends Master{
public static void main(String[] args){
String s=null;
try{ s=new Slave().doFileStuff();
}catch(Exception x) {
s= "b"; }
System.out.println(s);
}
//insert code here
}

Which, inserted independently at // insert code here will compile and produce the output b?(choose all that apply)

A. String doFileStuff() { return "b" ; }

b. String doFileStuff() throws IOException { return "b" ;}

c. String doFileStuff(int x) throws FileNotFoundException {return "b"; }

d. String doFileStuff() throws FileNotFoundException {return "b"; }

e. String doFileStuff() throws NumberFormatException { return "b"; }

f. String doFileStuff() throws NumberFormatException, FileNotFoundException{ return "b"; }

Answers :
A,D,E,F. Its okay for an overriding method to throw the same exceptions,narrower exceptions or no exceptions.And it's okay for overriding method to throw any runtime exceptions


Can you explain me the options "e" and "f"?.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

Can you explain me the options "e" and "f"?.

e. String doFileStuff() throws NumberFormatException { return "b"; }
f. String doFileStuff() throws NumberFormatException, FileNotFoundException{ return "b"; }


First of all, FileNotFoundException is checked exception.
Master class method declares this exception;

Sub class can do the following:
1- Does not declare any exception
2- Declares the same exception
3- Declares subclass exception that is subclass of the
parent class declared type exception
4- Declares/or throws any unchecked exception, as option "e" did,
NumberFormatException is unchecked exception.
5- Does combination of 4 and 5 (checked as well as unchecked exceptions)
as option "f" did.
[ April 30, 2007: Message edited by: Chandra Bhatt ]
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Thanks Chandra
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Exceptions doubt