unchecked exception

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hari krishnan
    New Member
    • Jun 2011
    • 5

    unchecked exception

    why unchecked exception raise while run this program. if i tack it out (throws Exeception ) this code will work well .


    Code:
    interface Foldable {
    	 public void fold() throws Exception ;
    
    }
    
    
    class Paper implements Foldable {
    	public void fold() { 
              System.out.print("Fold");
            }
    }
    
    
    public class Tester {
       public static void main(String args []) {
              Foldable obj1 = new Paper();
              obj1.fold(); 
              Paper obj2 = new Paper(); 
              obj2.fold();
       }
    }
    Last edited by Niheel; Jun 26 '11, 09:42 PM.
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    In short, every Exception has to be caught by a try-catch block or passed on to the next higher instance. This is true, even if there's just a possibility that an Exception might be thrown but never is.
    So, if you change the main function to
    Code:
    try {
       Foldable obj1 = new Paper();
       obj1.fold(); 
       Paper obj2 = new Paper(); 
       obj2.fold();
    } catch (Exception e) {
       System.err.println("Exception was thrown");
    }
    then you shouldn't have any problems.

    For more information, check out the article I wrote about exceptions.

    Greetings,
    Nepomuk

    Comment

    Working...