aspose file tools
The moose likes Threads and Synchronization and the fly likes How to throw exception in run method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "How to throw exception in run method" Watch "How to throw exception in run method" New topic
Author

How to throw exception in run method

babu krishnan
Greenhorn

Joined: Jun 09, 2002
Posts: 25
Hi,
In my thread class, when i invoke a run method, i can get an exception. I want this exception to be thrown to the method which called this thread..how do i do this..When i tried to throw exception, it is not compiling ..prompting that run method can't be overridden as throws clause has difference.
Anyone can help me in this! Thanks in advance...
If you have code samples please provide the same
thanks
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1112

Hi Babu,
There are two types of exception in java:
  • checked
  • unchecked

  • "checked" exceptions are checked at compile time.
    "unchecked" exceptions are not.
    Therefore your "run()" method needs to throw an unchecked exception. This section of the Java Tutorial has nore details:
    http://java.sun.com/docs/books/tutorial/essential/exceptions/throwable.html
    Basically, your "run()" method should throw an exception which is a subclass of "java.lang.RuntimeException"
    http://java.sun.com/j2se/1.4.1/docs/api/java/lang/RuntimeException.html
    Hope this helps.
    Good Luck,
    Avi.
    Mr. C Lamont Gilbert
    Ranch Hand

    Joined: Oct 05, 2001
    Posts: 1170

    I think an Exception is thrown upto the top of the thread in which it is thrown.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to throw exception in run method
     
    Similar Threads
    Propogating Exceptions - Exercise5.3 in K&B
    Method Signature
    Throwing Exceptions
    How can I handle exceptions in run()
    exceptions from run method