Results 1 to 16 of 16
Thread: chained exception
- 08-11-2010, 04:19 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 25
- Rep Power
- 0
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 } }What is the problem about this code?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)
- 08-11-2010, 04:23 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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
- 08-11-2010, 05:05 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Also, that's not my idea of a chained exception...but hey ho.
-
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.
- 08-11-2010, 05:22 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Last edited by JosAH; 08-11-2010 at 05:56 PM.
- 08-11-2010, 08:23 PM #6
Member
- Join Date
- Jan 2010
- Posts
- 25
- Rep Power
- 0
A
It was the answer for me!MyException extends an Exception which is a checked exception. You have to mention that fact in the start of your method: 'throws MyException'.
- 08-12-2010, 08:47 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 08-13-2010, 03:49 PM #8
Member
- Join Date
- Jan 2010
- Posts
- 25
- Rep Power
- 0
getcause must return the main exception that cause the exception. But it does not in this code.why?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] } } } }
- 08-13-2010, 03:55 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 08-13-2010, 06:35 PM #10
Member
- Join Date
- Jan 2010
- Posts
- 25
- Rep Power
- 0
It returns null
- 08-13-2010, 06:51 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 08-14-2010, 02:54 PM #12
Member
- Join Date
- Jan 2010
- Posts
- 25
- Rep Power
- 0
hey jo!
Now may you give me example in order to show me how to change that class to work?
- 08-14-2010, 03:03 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Last edited by JosAH; 08-14-2010 at 03:51 PM.
- 08-14-2010, 03:59 PM #14
Member
- Join Date
- Jan 2010
- Posts
- 25
- Rep Power
- 0
All right jo!
This is complete code:
Constructor is bold, Now what is the next stage?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() ) ; } } } }
- 08-14-2010, 04:09 PM #15
from post#11don't pass on the 'cause' exception to their superclass constructors ...
- 08-14-2010, 04:13 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Exception in thread "main" java.lang Exception In InitializerError
By kenzo2009 in forum New To JavaReplies: 4Last Post: 10-25-2010, 07:42 PM -
get exception name
By jithan in forum New To JavaReplies: 6Last Post: 06-05-2008, 02:29 PM -
Exception!
By rameshraj in forum Advanced JavaReplies: 1Last Post: 05-05-2008, 01:39 PM -
How to use chained exceptions
By Java Tip in forum java.langReplies: 0Last Post: 04-04-2008, 02:50 PM -
Trouble with factory method - unhandled exception type Exception
By desmond5 in forum New To JavaReplies: 1Last Post: 03-08-2008, 06:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks