• 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

Exceptions doubt

 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic