wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception Handling" Watch "Exception Handling" New topic
Author

Exception Handling

Narendhiran Nagarajan
Ranch Hand

Joined: Jun 17, 2009
Posts: 30
Should a checked exception be handled before the completion of the code?

As we know, the checked exceptions can be handled(try catch) or declared(throws).

For egs,

Class A{

public static void main(String[] args)
{
try{ m1();
}
catch(Exception e){ }
}
void m1() throws Exception{
m2();
}
void m2() throws Exception{
throw new Exception();
}

}

In this egs, I have handled the Exception in the main method.
If I m not handling, Is it possible throw the Exception in the main method???

public static void main(String[] args) throws Exception
{
m1();
}



Naren
-------------------
SCJP - 90%
SCWCD - 98%
Sridhar Gudipalli
Ranch Hand

Joined: Nov 02, 2005
Posts: 120
"...........Is it possible throw the Exception in the main method???............."

Yes. The below main method is legal.
public static void main(String[] args) throws Exception {
.........
}

Please refer:
http://www.coderanch.com/t/268027/Programmer-Certification-SCJP/certification/pulbic-static-void-main-String
http://rationalpi.wordpress.com/2007/01/29/main-methods-in-java/


Sridhar Gudipalli|SCJP 6.0
SCWCD objectives
Narendhiran Nagarajan
Ranch Hand

Joined: Jun 17, 2009
Posts: 30
But my ques is

"Is there any rule like- we need to handle checked exceptions before the completion of the code? "
Sridhar Gudipalli
Ranch Hand

Joined: Nov 02, 2005
Posts: 120
"......completion of the code"? Do you mean compilation of the code?

A compiler for the Java programming language checks, at compile time, that a program contains handlers for checked exceptions, by analyzing which checked exceptions can result from execution of a method or constructor. For each checked exception which is a possible result, the throws clause for the method (§8.4.4) or constructor (§8.8.4) must mention the class of that exception or one of the superclasses of the class of that exception. This compile-time checking for the presence of exception handlers is designed to reduce the number of exceptions which are not properly handled.

Please refer for more details : http://java.sun.com/docs/books/jls/second_edition/html/exceptions.doc.html
Narendhiran Nagarajan
Ranch Hand

Joined: Jun 17, 2009
Posts: 30
I mean, the till the end of stack(Main method).

I see that, we have to handle the checked exceptions.
Yes ,I agree.

Also, I understand that we can throw checked Exceptions fro main method

In which situation
the checked Exceptions are thrown from the main method.? (main method is the last chance to handle the chk excep)
mansi gupte
Ranch Hand

Joined: Dec 30, 2008
Posts: 72
Narendhiran Nagarajan wrote:I mean, the till the end of stack(Main method).

I see that, we have to handle the checked exceptions.
Yes ,I agree.

Also, I understand that we can throw checked Exceptions fro main method

In which situation
the checked Exceptions are thrown from the main method.? (main method is the last chance to handle the chk excep)



A checked exception should be declared and handled.This means that if a method say m1 declares an exception but does not handle it, it may be handled by m2, m3 0r m4 etc. methods within the same code.This 'passing the buck' situation is called Propagating Uncaught Exceptions.If an exception is not caught even by the main method which is at the bottom of the stack, then compiler issues displays the stack trace, and stops further execution of program.

Eg:

The following code throws an exception

Narendhiran Nagarajan
Ranch Hand

Joined: Jun 17, 2009
Posts: 30
Thanks for all your replies.
But I m sorry to ask the same question again.

Eventhough we can propagate Uncaught checked Exceptions, it should be handled before the main method moves out of the stack.

"So, In which situation the checked Exceptions are thrown from the main method? "
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Handling
 
Similar Threads
Class level lock versus Object level lock
Too many exceptions?
doubt in flow control in the folowing code !!!
Doubt in order of exception
Jq + Question ID :988380923984