• Post Reply Bookmark Topic Watch Topic
  • New Topic

Chained exception  RSS feed

 
Ranch Hand
Posts: 635
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator






What is the problem about this code?
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote: . . . What is the problem about this code?
You tell us the details and what is wrong.


I think I can see a problem, but we expect you to tell us such things.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want throw Myexception, I wan to practice Chained exception
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I change the 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)




But When I use ArithmeticException in this line:



It compiles without any problem why?

 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and what compiler error are you getting?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To the Batmobile Java™ Tutorials. You are particularly interested in the sections called
  • The Catch or Specify Requirement . . . and
  • Unchecked Exceptions — The Controversy
  • You also want to check the inheritance hierarchy of ArithmeticException
     
    Bartender
    Posts: 1561
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Crosspost: http://www.java-forums.org/new-java/31576-chained-exception.html

    abalfazl: Please read the FAQ including this one: BeForthrightWhenCrossPostingToOtherSites
     
    abalfazl hossein
    Ranch Hand
    Posts: 635
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dear pete

    I did that after I didn't get the answer here.
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    abalfazl hossein wrote:Dear pete

    I did that after I didn't get the answer here.
    No, you did that after you were given all the information required for your answer here.
     
    Ranch Hand
    Posts: 492
    Firefox Browser Linux VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you read the tutorial that Campbell linked you should know what that compile error means. Also, even if you didn't read the tutorial if you copy pasted the error into google, you'd get hits.

    "unreported exception exception throws.Exception; must be caught or declared to be thrown" returns several pertinent links.

    Hunter


     
    abalfazl hossein
    Ranch Hand
    Posts: 635
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No, you did that after you were given all the information required for your answer here.


    Dear Sir

    With all due respect Sir, I must inform you your idea was wrong.If I my question was answered here, It was not necessary to ask that in other forum

    I need that answer urgent.If you check the time that I post the question, You will see I posted after here, Not same time

    By the way, I appreciate your answers.Especially to my other questions, They were very helpful


    They you very mush
     
    abalfazl hossein
    Ranch Hand
    Posts: 635
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    getcause() finds the main exception that causes the exception.

    May you guide me how to use in in that example?Hpw to use initcause() method?
     
    Ranch Hand
    Posts: 3090
    14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think people in both threads had a hard time telling what you were asking.

    If you want to practice using chained exceptions, well your "Myexception" isn't a very good starting point because it doesn't have any constructors that accept another Throwable as a cause. You probably want something like this:

    The third and fourth constructors are the ones that allow chaining - they accept a Throwable parameter, and pass its value on to the super() constructor, which treats this Throwable as the thing that caused the current problem.

    If you don't provide at least one proper chaining constructor (like the third and fourth above) then you need to use initCause(Throwable). This is seldom used, because most exception classes have been retrofitted to use better constructors, and consequently many programmers do not even realize this alternative exists. But if you want to use it, you can. It's simple:

     
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    throw new Myexception();
    is a checked exception....you must have to catch it .......
    or let your main method throws it using throws clause...

    public static void main(String[] args) throws Myexception
     
    Shanky Sohar
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    check this code......


     
    Shanky Sohar
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    check this to know how it basically works.........


    http://www.coderanch.com/t/505848/java-programmer-SCJP/certification/polymorphism
     
    abalfazl hossein
    Ranch Hand
    Posts: 635
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    read,

    It was not helpful at all.
     
    Shanky Sohar
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    abalfazl hossein wrote:read,

    It was not helpful at all.



    really then you must have to study checked and unchecked exception...................
    after that everything will be clear...........
     
    Hunter McMillen
    Ranch Hand
    Posts: 492
    Firefox Browser Linux VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    For someone asking for help, you are not being very nice about it.

    Hunter
     
    pete stein
    Bartender
    Posts: 1561
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    abalfazl hossein wrote:Dear pete

    I did that after I didn't get the answer here.


    You didn't read the link I gave you then else you wouldn't give this answer. Read the link in the FAQ and you'll understand the issue: it's not the cross-post itself; it's the lack of notification.
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    abalfazl hossein wrote:read,

    It was not helpful at all.
    No, it wasn't at all helpful.
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    abalfazl hossein wrote: . . .
    Dear Sir

    With all due respect Sir, I must inform you your idea was wrong.If I my question was answered here, It was not necessary to ask that in other forum

    I need that answer urgent.If you check the time that I post the question, You will see I posted after here, Not same time

    By the way, I appreciate your answers.Especially to my other questions, They were very helpful


    They you very mush
    We don't recognise the word urgent here.
    Hunter McMillen wrote:If you read the tutorial that Campbell linked you should know what that compile error means. Also, even if you didn't read the tutorial if you copy pasted the error into google, you'd get hits.

    "unreported exception exception throws.Exception; must be caught or declared to be thrown" returns several pertinent links.

    Hunter
    You have indeed been given the information required. If you had read the links, the answer would be obvious.
     
    Shanky Sohar
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    now i will spilt your program into three post see each and every post......clearly............if still not able to understand then tell me.........
    i will again be ready to help you.............
     
    Shanky Sohar
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator



    this code runs fine because here you have thrown the ArithmeticException is a runtime exception and it is not checked by compiler.....
    and will be thrown by a JVM.......
     
    Shanky Sohar
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    As Myexception extends Exception so it is checked exception which is being checked by the compiler.....
    and if any block of code is throwing this exception then it should be put in a try/catch block...
    see the below example if not able to understand how....




    as method1 is calling method2 so it is being declared to catch this exception.......


    and also
    if any method is throwing this exception then the method which is calling this method will be able to throw the exception
    like
    if


    as method1 is calling method2 then method1 should be declare to throws myexception as when method2 throws the exception it will be propagated to method1.......

     
    Shanky Sohar
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator



    so in your this code



    so in your code
    throw new Myexception("Myexcpetion thrown",a);.
    this is a checked exception so when we throw it it will be checked by compiler whther someone who is calling the main method is able to catch the exception or not
    if not then it will give compile time error........as there is no method which is calling this main method......so this is not able to catch..

    so you can also do this......


    also as in your code.
    Myexception("Myexcpetion thrown",a); this construtor is also not defined
    so defined this constructor as

     
    Shanky Sohar
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    complete code looks like this.........

    one ways is........



    second way is...........




     
    Shanky Sohar
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i tried my best to make it clear................i think it must be cleared now

     
    abalfazl hossein
    Ranch Hand
    Posts: 635
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your answers were the best!

    Especially this code:
     
    Shanky Sohar
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    abalfazl hossein wrote:Your answers were the best!


    Thanks..............so now i think its clear now........
     
    abalfazl hossein
    Ranch Hand
    Posts: 635
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    getcause must return the main exception that cause the exception. But it does not in this code.why?
     
    Ranch Hand
    Posts: 46
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    abalfazl hossein wrote: getcause must return the main exception that cause the exception. But it does not in this code.why?


    The only reason is, shanky sohar showed a sample customized exception class (for your reference) and not fully developed customized exception class. In order to make the getCause work all you have to do is to develop your MyException class properly.



    In fact, what Mike Simmons said was precisely correct. A fully grown customized exception class should have all these methods. Just add this MyException class in shanky sohar code and all will work as per your wish.

     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!