Exception Handling

J

Javas

public class Qn24 {

static class A
{
void process() throws Exception { throw new
Exception(); }
}


static class B extends A
{
void process() { System.out.println("B "); }
}


public static void main(String[] args)
{
A a = new B();
a.process();
}



}


Why does the above code results in a compilation error? since it is
always invoking class B's process() method which doesnt throw any
exception.
 
M

markspace

Javas said:
static class A
{
void process() throws Exception { throw new ....
A a = new B();
a.process();
Why does the above code results in a compilation error? since it is
always invoking class B's process() method which doesnt throw any
exception.


Well, I'm not an expert, but Java's type system is often called "static"
by those who are. In other words, Java doesn't look to see what type is
actually assigned to "a". It just sees that a is type A, and that A
could throw an exception. So from a point of view that is strictly
static in type, it seems very reasonable to me that Java says an
exception could be thrown there.
 
L

Lew

Javas said:
public class Qn24 {

Say, I'm just taking a WAG here, but this wouldn't happen to be question
number 24 on your homework assignment, would it?
static class A
{
void process() throws Exception { throw new
Exception(); }

Do us a favor here on Usenet and please keep indentation down to a maximum of
four spaces per level. Eight is too wide for readability in a newsgroup.
}

static class B extends A
{
void process() { System.out.println("B "); }
}

public static void main(String[] args)
{
A a = new B();
a.process();
}
}

Why does the above code results in a compilation error? since it is

When asking questions like this, saying "a compilation error" is waaaaay to
vague. What is the error? *Copy and paste* the *exact* text of the error in
these cases.
always invoking class B's process() method which doesnt throw any
exception.

The key is that it's a compilation error, not a runtime one. The fact that
a.process() is polymorphically calling B's version is a runtime consideration,
not a compilation one. See markspace's answer.

It becomes quite important in Java to keep compile-time and run-time realities
separate in your mind.
 
L

Lew

Please reply to the list.

Here is what you sent, with attributions:
> > Say, I'm just taking a WAG here, but this wouldn't happen to be question
> > number 24 on your homework assignment, would it?
> > Do us a favor here on Usenet and please keep indentation down to a maximum of
> > four spaces per level. Eight is too wide for readability in a newsgroup.
>> > > } > >
>> > > static class B extends A
>> > > {
>> > > void process() { System.out.println("B "); }
>> > > } > >
>> > > public static void main(String[] args)
>> > > {
>> > > A a = new B();
>> > > a.process();
>> > > }
>> > > } > >
>> > > Why does the above code results in a compilation error? since it is
Lew:
> > When asking questions like this, saying "a compilation error" is waaaaay to
> > vague. What is the error? *Copy and paste* the *exact* text of the error in
> > these cases.
Lew:
> > The key is that it's a compilation error, not a runtime one. The fact that
> > a.process() is polymorphically calling B's version is a runtime consideration,
> > not a compilation one. See markspace's answer.
> >
> > It becomes quite important in Java to keep compile-time and run-time realities
> > separate in your mind.
Guruji! Don't try to act smart. Just try to share your views and
convey your requests in a cordial mannar. Hope you understand your
mistakes. I'll correct me from my next post onwards. Anyways, Thanks
for your answer.

#1: If you're so smart, why are you asking us for help?

#2: What wasn't cordial about my manner?

#3: What mistakes? Trying to help you, apparently.
 
J

Javas

Please reply to the list.

Here is what you sent, with attributions:

Javas wrote:

 >> > > public class Qn24 {

Lew wrote:

 > > Say, I'm just taking a WAG here, but this wouldn't happen to be question
 > > number 24 on your homework assignment, would it?

Javas wrote:

 >> > >         static class A
 >> > >         {
 >> > >                 void process() throws Exception { throw new
 >> > > Exception(); }

Lew wrote:

 > > Do us a favor here on Usenet and please keep indentation down to a maximum of
 > > four spaces per level.  Eight is too wide for readability in a newsgroup.

Javas wrote:

 >> > >         }
 > >
 >> > >         static class B extends A
 >> > >         {
 >> > >                 void process() { System.out.println("B "); }
 >> > >         }
 > >
 >> > >         public static void main(String[] args)
 >> > >         {
 >> > >                 A a = new B();
 >> > >                 a.process();
 >> > >         }
 >> > > }
 > >
 >> > > Why does the above code results in a compilation error? since it is

Lew:
 > > When asking questions like this, saying "a compilation error" is waaaaay to
 > > vague.  What is the error?  *Copy and paste* the *exact* text of the error in
 > > these cases.

Javas wrote:

 >> > > always invoking class B's process() method which doesnt throw any
 >> > > exception.

Lew:
 > > The key is that it's a compilation error, not a runtime one.  The fact that
 > > a.process() is polymorphically calling B's version is a runtime
consideration,
 > > not a compilation one.  See markspace's answer.
 > >
 > > It becomes quite important in Java to keep compile-time and run-time
realities
 > > separate in your mind.
Guruji! Don't try to act smart. Just try to share your views and
convey your requests in a cordial mannar. Hope you understand your
mistakes. I'll correct me from my next post onwards. Anyways, Thanks
for your answer.

#1:  If you're so smart, why are you asking us for help?

I never advertised to you that I am smarter than
you. I guess you are trying to get personal.
#2:  What wasn't cordial about my manner?

You could have just answered the questions and
stopped there, instead of worrying about whether's it my homework or
assignment work. Or atleast you could have suggested me to name the
class properly. But you chose the wrong path.
#3:  What mistakes?  Trying to help you, apparently.

You had some other intentions too other than
helping one alone.

Above all, I just wanted to convey it to you.
That's the reason why I sent it to you alone.
 
R

RedGrittyBrick

Eric Raymond collected some notes on getting the best out of Usenet. I
found them useful, maybe you will too. References below.
I never advertised to you that I am smarter than
you. I guess you are trying to get personal.
http://www.catb.org/~esr/faqs/smart-questions.html#not_losing



You could have just answered the questions and
stopped there, instead of worrying about whether's it my homework or
assignment work. Or atleast you could have suggested me to name the
class properly. But you chose the wrong path.
http://www.catb.org/~esr/faqs/smart-questions.html#homework



You had some other intentions too other than
helping one alone.

Above all, I just wanted to convey it to you.
That's the reason why I sent it to you alone.

http://www.catb.org/~esr/faqs/smart-questions.html#noprivate
 
J

Joshua Cranmer

Why does the above code results in a compilation error? since it is
always invoking class B's process() method which doesnt throw any
exception.

How can the compiler know that you're only invoking B's method?
 
L

Lew

I never advertised to you that I am smarter than you.

Nor did I.
I guess you are trying to get personal.

And you weren't?
You could have just answered the questions and
stopped there, instead of worrying about whether's it my homework or
assignment work. Or atleast you could have suggested me to name the
class properly. But you chose the wrong path.

You did name the class properly, afaict, and what is wrong about my "path"?
Who said I was "worried"? This is a discussion group, not your little unpaid
help-slave labor pool; I was discussing. Helpfully, too, though you missed that.

Lew:
You had some other intentions too other than
helping one alone.

Oh? You misread my mind there, Kreskin. Everything I said in my first
response was to help you.
Above all, I just wanted to convey it to you.
That's the reason why I sent it to you alone.

Usenet is a public forum for open discussion.
 
J

Jeff KE7NVY

public class Qn24 {

        static class A
        {
                void process() throws Exception { throw new
Exception(); }
        }

        static class B extends A
        {
                void process() { System.out.println("B "); }
        }

        public static void main(String[] args)
        {
                A a = new B();
                a.process();
        }

}

Why does the above code results in a compilation error? since it is
always invoking class B's process() method which doesnt throw any
exception.

Exception is a checked exception. That means that whenever you
declare
a method that (potentially) throws it, you must do one of two things
in
the code that calls that method. Either handle the exception by
putting
the call inside a try/catch block:

public static void main(String[] args) {
A a = new B();
try {
a.process();
}
catch (Exception e) {
System.out.println("main: Exception caught");
}
}

Or, declare the calling method as also throwing Exception:

public static void main(String[] args) throws Exception {
A a = new B();
a.process();
}
 
J

Joshua Cranmer

In this case the compiler *could* know through data flow analysis. The
method invocation is dominated by the assignment of the result of a B
instance creation to the invocation's target reference.

I'm not aware of any statically-typed language that would resolve typing
through data flow analysis, although my knowledgeable set of languages
is rather slim.
In order to avoid situations in which a program is considered valid by
one compiler but rejected by another, the compile time error rules are
written up in the JLS. Allowing the example program would require a set
of rules at least as complicated as the definite assignment rules at
http://java.sun.com/docs/books/jls/third_edition/html/defAssign.html.

I would imagine such a specification would have to bring in the computed
types of variables and define some sort of SSA-like mechanism to compute
computed types. There are also a few expressions, at least in Java 1.5,
whose type is dependent on the type of the variable they're being
assigned to (see
<http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.2.8>).

On second thought, the interaction of computed types and generics would
probably get very ugly. Given the amount of WTF moments when working
with generics, adding more would be counterproductive.

In statically-typed languages, then, I'm all for keeping the typing that
matters for method lookup the same as the declared type of the variable.
That's why we declared it, after all.
 
J

Jeff KE7NVY

Jeff said:
public class Qn24 {
        static class A
        {
                void process() throws Exception { throw new
Exception(); }
        }
        static class B extends A
        {
                void process() { System.out.println("B "); }
        }
        public static void main(String[] args)
        {
                A a = new B();
                a.process();
        }
}
Why does the above code results in a compilation error? since it is
always invoking class B's process() method which doesnt throw any
exception.
Exception is a checked exception.  That means that whenever you
declare
a method that (potentially) throws it, you must do one of two things
in
the code that calls that method.  Either handle the exception by
putting
the call inside a try/catch block:
public static void main(String[] args) {
    A a = new B();
    try {
        a.process();
    }
    catch (Exception e) {
        System.out.println("main: Exception caught");
    }
}

I don't like code that catches Exception and neither re-throws it,
possibly wrapped with additional data, nor terminates the program. If
this main method were called from another class, rather than being used
as the top method of the whole application, the caller could be left
continuing to execute with the processing not done and no indication
that something went wrong other than a line in standard out.

Absolutely true, and I agree completely. I was just trying to
illustrate why the compiler was unhappy. The OP is correct in that,
when you run this, B.process() will be called. But before you can run
it, you have to make the compiler happy. When the compiler reaches
the line

a.process();

in main(), all it knows is that you declared 'a' as an A object, so it
assumes that you're calling A.process(). And since A.process()
potentially throws the Exception, you need to either handle it locally
or pass it on up to the calling code.
 
A

Arne Vajhøj

You could have just answered the questions and
stopped there, instead of worrying about whether's it my homework or
assignment work.

He could.

If he were mean enough to do it.

Nothing is more cruel than supplying the answers to
homework, so that the students does not learn what they
need to learn.

Arne
 
A

Arne Vajhøj

I don't mind answering homework questions. The more dummies who never
learned to think in the class room, the more job opportunities for me.

Tsk tsk

Arne
 
D

Daniel Pitts

I don't mind answering homework questions. The more dummies who never
learned to think in the class room, the more job opportunities for me.
Unfortunately, it may also be the bigger the dummy you have to report to.

I've been lucky with my managers, but not everyone is.
 
J

Joshua Cranmer

Nothing is more cruel than supplying the answers to
homework, so that the students does not learn what they
need to learn.

And nothing is sadder than doing so and watching the student still make
the same mistake on the test.

On a related note: what is so hard to understand about addressability
and address space?
 
A

Arne Vajhøj

And nothing is sadder than doing so and watching the student still make
the same mistake on the test.
:-(

On a related note: what is so hard to understand about addressability
and address space?

You lost me.

Arne
 
L

Lew

Probably overly harsh on my part, but still, perhaps the OP will take
the point.

I doubt it, given his harsh reaction to simply being asked if it was homework,
without any criticism attached to the question. That doesn't really seem like
the action of someone who'd take the point.
 
A

Arne Vajhøj

I doubt it, given his harsh reaction to simply being asked if it was
homework, without any criticism attached to the question. That doesn't
really seem like the action of someone who'd take the point.

True.

But then: his problem.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top