• 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

Exception Handling

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"...........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:
https://coderanch.com/t/268027/Programmer-Certification-SCJP/certification/pulbic-static-void-main-String
http://rationalpi.wordpress.com/2007/01/29/main-methods-in-java/
 
Narendhiran Nagarajan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"......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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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? "
 
reply
    Bookmark Topic Watch Topic
  • New Topic