Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception problem." Watch "Exception problem." New topic
Author

Exception problem.

padmini Babu
Ranch Hand

Joined: Feb 10, 2001
Posts: 103
Well, i have two pieces of code, both are of similar kind.
But the above class checkExcept throws a compile error that::
" Exception java.lang.Exception must be caught, or it must be
declared in the throws clause of this method. "
whereas the class TestClass9 gives me no compile eror but the follwing print statements and a runtime excption:
"
/*
G:\paddy\java\Jqplus590quest\jqplus\jj\Referencequest>java TestClass9
m1 Starts
1
4
Exception in thread "main" java.lang.NullPointerException
at TestClass9.main(TestClass9.java:9)
xxxx
Problem: Can anyone explain me why this difference exists?
public class checkExcept
{
public static void main(String args[])
{
try
{
Exception e = null;
}
catch(Exception e)
{
throw e; .
}
System.out.println("This excpetion is caught ");
}
}

xxxx
public class TestClass9
{
public static void main(String args[])
{
try{
m1();
}catch(IndexOutOfBoundsException e){
System.out.println("1");
throw new NullPointerException();
}catch(NullPointerException e){
System.out.println("2");
return;
}catch (Exception e) {
System.out.println("3");
}finally{
System.out.println("4");
}
System.out.println("END");
}
// IndexOutOfBoundsException is a subclass of RuntimeException.
static void m1()
{
System.out.println("m1 Starts");
throw new IndexOutOfBoundsException( "Big Bang " );
}
}

[This message has been edited by padmini Babu (edited June 15, 2001).]
Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
http://www.javaranch.com/ubb/Forum24/HTML/010321.html
Seems pretty similar to a question I was having. I believe if a method throws a checked exception, then all statements that call the method must catch it or give compiler error.
padmini Babu
Ranch Hand

Joined: Feb 10, 2001
Posts: 103
Hello Steven,
Well, mine is throwing a nullpointerexception which is a non-checked exception.
And , I found this information from Simon Roberts Book:
Well, it states that:
____________________________________________________
For checked exceptions, you can put a try block around the code that might throw the exception and provide a corresponding catch block that will apply to the exception in question. This handles the exception effectively and so it goes away.
Alternatively, you might decide that if this exception occurs , your method cannot proceed and should be abandoned. In this case, you do not need to provide a try/catch construction, but you must instead make sure that the method declaration indcludes a throws part that informs potential callers that the exception might arise. Notice that by insisting that the method be declared in this way, the responsibility of handling the eception is explicitly passed to the caller of the method, which must then make the same choice -- whether to declare or handle the exception "" ..
Please refer to the last sentence -- declare or handle the exception, ie, i think the exception can be put under try/catch at the place where is the method is called or can be declared using throws again . which i presume shd not give a compile error.
However , I am not sure how this code works. I am myself confused.
It would also be helpful , if the code is provided along with the expln.
Anyone, who knows please provide more information.
Thanks in advance, padmini
padmini Babu
Ranch Hand

Joined: Feb 10, 2001
Posts: 103
Hello steve,
In your code that you have redirected me to :
If i put " throws ClassNotFoundException " in line 25 because the control flows to the calling method, I get an error
" RR.java:20: ')' expected.
System.out.print( rr1.methodB() throws ClassNotFoundException ); //
line 25
"
Why is this error?
Is it that throws clause can only be given in called methods and not calling methods?
Daniel Wu
Ranch Hand

Joined: Jun 13, 2001
Posts: 58
Originally posted by padmini Babu:
[B]Well, i have two pieces of code, both are of similar kind.
But the above class checkExcept throws a compile error that::
" Exception java.lang.Exception must be caught, or it must be
declared in the throws clause of this method. "
Hi,
I think AN EXCEPTION E IS THROWN IN YOUR CATCH BLOCK WHICH CAUSE ERROR BECAUSE THIS NEW EXCEPTION IS NOT HANDLED (using "throws" or try/catch). Coudl anyone comment on my point?
whereas the class TestClass9 gives me no compile eror but the follwing print statements and a runtime excption:
"
/*
G:\paddy\java\Jqplus590quest\jqplus\jj\Referencequest>java TestClass9
m1 Starts
1
4
Exception in thread "main" java.lang.NullPointerException
at TestClass9.main(TestClass9.java:9)
xxxx
Hi, here,
If NullPointerException is not a checked exception, then its occurence will cause a runtime error. Programmer should write code carefully to avoid non-checked exception which can't be handled by using throws or try/catch. Right?
Daniel
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Programmer should write code carefully to avoid non-checked exception which can't be handled by using throws or try/catch. Right?
All exceptions, either checked or unchecked, can be caught by try-catch-finally statement. It is not recommended to catch unchecked exceptions, because they represent flaws in a program itself, which can and should be eliminated. Unchecked exceptions can be thrown and caught during debugging, but they are not supposed to appear and, therefore, are not supposed to be caught, in a final release. However, the compiler will not object if you throw or catch them.

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
padmini Babu
Ranch Hand

Joined: Feb 10, 2001
Posts: 103
Originally posted by Daniel Wu:
Hi,
I think AN EXCEPTION E IS THROWN IN YOUR CATCH BLOCK WHICH CAUSE ERROR BECAUSE THIS NEW EXCEPTION IS NOT HANDLED (using "throws" or try/catch). Coudl anyone comment on my point?
__________________
Well Daniel,
Even in the class checkexcpt also, the exception e that is thrown is a NullPointerException which is a non-checked Exception and Non-checkedExceptions are not required to be caught or declared.
Padmini
__________

Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Padmini, in your first example

You are throwing e (a checked exception) without either catching it directly or declaring that its enclosing method (main) throws an Exception. Thus, the code won't compile, and Daniel is correct in the point he makes.
In your second example

You also fail to either catch the NullPointerException or declare it in the enclosing method. However, because NullPointerException is an unchecked exception, it is not required that you do so (nor is it recommended, as Maraprupta said). The code will compile fine. When you run it, of course, it throws the exception and halts the program, as you can see in your output.
The other posts have said much the same thing; I just wanted to summarize it so that it directly answers your original question.
padmini Babu
Ranch Hand

Joined: Feb 10, 2001
Posts: 103
Originally posted by Scott Appleton:
[B]Padmini, in your first example
[CODE]
_____________________________________________________
Thank you very much Scott. Now , the two examples are clear to me. Thanks, Daniel, Mapraputa and steve.
I just missed out on one point and that is:
Exception e = null; I presumed since e must be throwing a NullPointerException and hence I was going round and round. But when I actually, put a System.out.println(e) inside the catch only then I found out that there was no exception at all. I mean the
Exception e = null ; is not an exception at all and hence it was not going to the catch block.
Well, Still I have one more point to clarify:
Exception e = null; - Is there not a rule that anything pointing to null would throw a NullPointerException. Then how come , here the situation is different?
I am also not very sure when NullPointerException occurs. I have gone through the API regarding this. But i have not come accross many examples. Can anybody comment on this, please?
Thanks in advance,
Padmini
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Padmini, you can set the value of any object reference to null, as your example is doing. A NullPointerException will occur when you try to use this reference in a way that requires a "real" (non-null) object as a reference, such as calling a method or referencing a variable for this object.
Example:
Thread t = null; //fine
t.start(); // throws NullPointerException
int y = t.someInt; //throws NullPointerException
[This message has been edited by Scott Appleton (edited June 18, 2001).]
padmini Babu
Ranch Hand

Joined: Feb 10, 2001
Posts: 103
Originally posted by Scott Appleton:
[B]
Thanks scott,
The message is very clear to me now.
Thank you very much.
Padmini
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception problem.