Results 1 to 16 of 16
  1. #1
    simorgh is offline Member
    Join Date
    Jan 2010
    Posts
    25
    Rep Power
    0

    Default chained exception

    Java Code:
        
    
    public class ExceptionDemo
     {
         int i;
    
         public ExceptionDemo(int i)
         {
             this.i = i;
        }//end constructor
    
      void divideByZero()throws ArithmeticException  {
          if(i==0)
              throw new ArithmeticException("zero divide error");
          System.out.println(12 / i);
        }//end divide . . .
    }
    
    public class Myexception extends Exception {
        public Myexception(){
            System.out.println("Chained Exception");
        }
    
    }
    
    
    public class Main {
    
          public static void main(String[] args) {
          try
             {
                 ExceptionDemo ed = new ExceptionDemo(0);
                 ed.divideByZero();
            }//end try
             catch (ArithmeticException a)
            {
                  System.out.println("zero divide error");
                  throw new Myexception();
             }//end catch
         }
        }
    Compiling 1 source file to C:\Documents and Settings\Administrator\Chained\build\classes
    C:\Documents and Settings\Administrator\Chained\src\exceptionthrows \Main.java:22: unreported exception exceptionthrows.Myexception; must be caught or declared to be thrown
    throw new Myexception();
    1 error
    BUILD FAILED (total time: 0 seconds)
    What is the problem about this code?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default

    A MyException extends an Exception which is a checked exception. You have to mention that fact in the start of your method: 'throws MyException'.

    kind regards,

    Jos

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Also, that's not my idea of a chained exception...but hey ho.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Crosspost: Chained exception (Beginning Java forum at JavaRanch)

    Please do not crosspost without notifying all threads of the crosspost. The problem is that you're asking unpaid volunteers to help you here, not paid consultants, and most of us mind if we take time away from work/lives/play to try to help someone only to find that we've provided a solution that was already provided in a cross-post hours ago. The proper thing to do is that if you feel you must cross-post, at least provide links in each cross-post to the other, so that folks don't waste time answering something already answered and so that folks can review all discussions that have already occurred.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    hey ho.
    Hey ho? As in "hey ho, hey ho, it's off to work we go!" or as in "ho ho ho!". In both cases that's silly because your name isn't one of Doc, Grumpy, Happy, Sleepy, Bashful, Sneezy or Dopey; in the other case, it's August for pete's sake!

    kind regards,

    Jos ;-)
    Last edited by JosAH; 08-11-2010 at 06:56 PM.

  6. #6
    simorgh is offline Member
    Join Date
    Jan 2010
    Posts
    25
    Rep Power
    0

    Default

    A
    MyException extends an Exception which is a checked exception. You have to mention that fact in the start of your method: 'throws MyException'.
    It was the answer for me!

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    Hey ho? As in "hey ho, hey ho, it's off to work we go!" or as in "ho ho ho!". In both cases that's silly because your name isn't one of Doc, Grumpy, Happy, Sleepy, Bashful, Sneezy or Dopey; in the other case, it's August for pete's sake!

    kind regards,

    Jos ;-)
    I was rather tired yesterday, so Sleepy wouldn't have been far off.

    As for "ho ho ho", I bet they'll be promoting stuff for Christmas before long!

  8. #8
    simorgh is offline Member
    Join Date
    Jan 2010
    Posts
    25
    Rep Power
    0

    Default

    Java Code:
    public class Main {
    
          public static void main(String[] args) {
               try{
                   ExceptionDemo ed = new ExceptionDemo(0);
                   ed.divideByZero();
               }
             
               catch (ArithmeticException a){
                  try{
                  System.out.println("zero divide error");
                  throw new Myexception("Chained Exception",a );
                  }
                  catch(Myexception ex){  
                  System.out.println("catched checked exception");
                 [b] System.out.println ( "Cause is:\n" + ex.getCause()  ) ;[/b]
               
                  } 
               }
             
         }
        }
    getcause must return the main exception that cause the exception. But it does not in this code.why?

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by simorgh View Post
    getcause must return the main exception that cause the exception. But it does not in this code.why?
    The obvious question is: what does it return?

    kind regards,

    Jos

  10. #10
    simorgh is offline Member
    Join Date
    Jan 2010
    Posts
    25
    Rep Power
    0

    Default

    It returns null

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by simorgh View Post
    It returns null
    That figures; show us the constructors of your MyException class; I bet that they don't pass on the 'cause' exception to their superclass constructors ...

    kind regards,

    Jos

  12. #12
    simorgh is offline Member
    Join Date
    Jan 2010
    Posts
    25
    Rep Power
    0

    Default

    hey jo!

    Now may you give me example in order to show me how to change that class to work?

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by simorgh View Post
    hey jo!

    Now may you give me example in order to show me how to change that class to work?
    First you show us the constructor(s) of your exception class and I'll show you the mistake you made.

    On second thought: forget about it because your question has been answered (several times!) in your crosspost in the JavaRanch forum.

    kind regards,

    Jos
    Last edited by JosAH; 08-14-2010 at 04:51 PM.

  14. #14
    simorgh is offline Member
    Join Date
    Jan 2010
    Posts
    25
    Rep Power
    0

    Default

    All right jo!

    This is complete code:

    Java Code:
    public class ExceptionDemo
     {
         int i;
    
         public ExceptionDemo(int i)
         {
             this.i = i;
        }//end constructor
    
      void divideByZero()throws ArithmeticException  {
          if(i==0)
              throw new ArithmeticException("zero divide error");
          System.out.println(12 / i);
        }//end divide . . .
    
    public class Myexception extends Exception {
        [B]public Myexception(String s, Exception e){
            System.out.println(s);
            
        }[/B]
    
    }
    
    import java.lang.String;
    import java.lang.Exception;
    /**
     *
     * @author Administrator */
    public class Main {
    
          public static void main(String[] args) {
               try{
                   ExceptionDemo ed = new ExceptionDemo(0);
                   ed.divideByZero();
               }
             
               catch (ArithmeticException a){
                  try{
                  System.out.println("zero divide error");
                  throw new Myexception("Chained Exception",a );
                  }
                  catch(Myexception ex){  
                  System.out.println("catched checked exception");
                  System.out.println ( "Cause is:\n" + ex.getCause()  ) ;
                  
                 } 
               }
             
         }
        }
    Constructor is bold, Now what is the next stage?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    don't pass on the 'cause' exception to their superclass constructors ...
    from post#11

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by simorgh View Post
    Constructor is bold, Now what is the next stage?
    That answer has been given to you in your other crossposted thread on the JavaRanch forum; I am not going to repeat it here. All you have to do is read those answers.

    kind regards,

    Jos

Similar Threads

  1. Replies: 4
    Last Post: 10-25-2010, 08:42 PM
  2. get exception name
    By jithan in forum New To Java
    Replies: 6
    Last Post: 06-05-2008, 03:29 PM
  3. Exception!
    By rameshraj in forum Advanced Java
    Replies: 1
    Last Post: 05-05-2008, 02:39 PM
  4. How to use chained exceptions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-04-2008, 03:50 PM
  5. Replies: 1
    Last Post: 03-08-2008, 07:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •