• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

IOException confusion

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SOURCE :: http://www.valoxo.ch/jr/mocks/mock01a.html
(Link from javaranch only)



Doubts:
1.Why does this give an error --- "Unreacheable catch block for IOException. This Exception is never thrown from the try statement."
2.Why does it compile if I change the IOException in catch to ArithmeticException or just plain Exception or any other exception.
3.Is there some special functionality or feature of IOException that I am not aware of.
[ April 10, 2008: Message edited by: Rajat Asani ]
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not a special functionality of IOException.

IOException is an Checked Exception.
ArithmethicException, NullPointerException etc are Unchecked Exception.

You cannot put the catch block for Checked Exception unless some method calls of try block or try block throws the same exception.

In your code try block contains only myref.test() but it is not throwing IOException, So it fails..
[ April 10, 2008: Message edited by: Balasubramani Dharmalingam ]
 
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.io.*;
public class Question05 {
public static void main(String[] args) {
Question05Sub myref = new Question05Sub();
try{
myref.test();
}catch(IOException ioe){}
}void test() throws IOException{
System.out.println("In Question05");
throw new IOException();
}}
class Question05Sub extends Question05 {
void test() {
System.out.println("In Question05Sub");
}}

--------------------------------------------------------------------------------



Doubts:
1.Why does this give an error --- "Unreacheable catch block for IOException. This Exception is never thrown from the try statement."
2.Why does it compile if I change the IOException in catch to ArithmeticException or just plain Exception or any other exception.
3.Is there some special functionality or feature of IOException that I am not aware of.


Hi,

Let me try explaining it, I think my assumptions are correct.

Doubt 1 : When I compiled your code I got a compile time error :

exception java.io.IOException is never thrown in body of corresponding try statement
}catch(IOException ioe){}

Reason is : we are creating an object of a subclass which has test() method that does not throw any exception, so it is obvious from the error message that compiler does not expect an extra work done by the code to handle the erro which is surely never thrown.

If you change the method declaration of test() in sybclass as

void test() throws IOException{.... }

Program compiles perfectly fine. It is because you are creating an object of child class and hence child class method test() will be called.

If you create an object like this
Question05Sub myref = new Question05();

It compiles fine and runs due to DMD.

Doubt 2 & 3: It compiles when you change IOException to ArithmeticException, as ArithmeticException is Runtime Unchecked Exception category. Unchecked exceptions are not checked at compile time. BUT IOException is a checked Exception and hence is checked at compile time.
All checked exceptions either should be thrown or caught. Another example of checked exception is InterruptedExcption.

I hope it clear your doubt.
 
Rajat Asani
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys,

Dont know why but the point doesnt strike when required

Anyways can you please tell me, where can i find a list of exceptions(checked/unchecked) that we must know about for the exam.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajat,

I dont think you need to go through 'em all to get the exam.
Some are the ones you encounter....like NullPointerException...ClassCastException n all...these come from coding experience. All that needs to be known is covered in Kathy Sierra.

i think the exam expects us to be familiar with the most common ones and the difference between Checked and Unchecked ones and syntax issues and the flow in case of caught and uncaught exceptions.

Thanks,
Sandy
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have two questions:

One:

If you create an object like this
Question05Sub myref = new Question05();

It compiles fine and runs due to DMD.



When i tried the above i got a compiler error:

Question05.java:5: incompatible types
found : Question05
required: Question05Sub
Question05Sub myref = new Question05();

and can you explain what is DMD?
*****************************************************************
Two:

import java.io.*;
public class Question05 {
public static void main(String[] args) {
Question05Sub myref = new Question05Sub();
try{
myref.test();
}catch(IOException ioe){ ioe.printStackTrace();}
}
void test() throws IOException{
System.out.println("In Question05");
throw new IOException();
}
}
class Question05Sub extends Question05 {
void test(){
System.out.println("In Question05Sub");

}
}



Another question from the above code :
In K&B page 102/103 it states
"Bottom line: an overriding method doesn't have to declare any exceptions that it will never throw, regardless of what the overridden method declares."
Based on the above statement the above code should compile just fine but it doesn't? But if you declare the same exception as declared by the overridden method or a subclass of that exception it compiles fine. So I think either the statement made
K&B is wrong or i have not understood the concept. Please somebody throw some light. Thanks
 
Nancy Antony
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question05 myref = new Question05Sub();

Sorry it is like this. Parent class object refernce can be used to hold the child class object. When a method is called this parent ref. can call the parent class methods which are not overriden and the overriden methods of child class, but cannot call the methods of child class. Which method has to be called by the parent obj. ref is taken at runtime and is called Dynamic Method Dispatch. (Ref Java Complete Reference by Herbert Schildt)

ABOUT SECOND QUESTION

see the code below...it compiles

import java.io.IOException;
class ThrowsInOverriding{

int x;

public void test() throws IOException{
System.out.println("Parent Test");
throw new IOException();
}
public void test1(){}
}

class ChildThrowsNot extends ThrowsInOverriding {
int y;

public void test(){
System.out.println("Child Test");
}
public void test2(){}
}

Book is just talking about the possibility and not how we are going to use it. What do you say? Does it make sense?

Nancy
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Book is just talking about the possibility and not how we are going to use it. What do you say? Does it make sense?



Thanks Nancy for the reply. I think i get what you are trying to say. It does make sense.
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.io.*;
public class Question5
{
public static void main(String[] args)
{
Question05Sub myref = new Question05Sub();
try
{
myref.test();
}
catch(ArithmeticException ioe){}
}
void test()
{
System.out.println("In Question05");

}
}
class Question05Sub extends Question5
{void test()
{
System.out.println("In Question05Sub");
}
}


i have corrected the code , also we need to remove the expection throw new IOException.
o/p -- In Question05Sub

Thanks
 
Get me the mayor's office! I need to tell him about this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic