Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt About Is Catch block Is Necessary After try block. . .

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,


According to Syntax.. There is no need to have both catch and finally block after try block.

when i compile above code, it shows "unreported exception java.lang.InterruptedException must be caught or declared to be thrown"

Anyone can tell me why this is happen even i write finally after try and declared a main throws Exception ?
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I may be wrong, but you have the exception in the run() method, so it should be handled or declared just there.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'cause its a checked exception.
All checked exceptions(all exceptions excluding those subclassed from RuntimeException)must be caught or declared to be thrown. In other words, all classes extending RuntimeException class are unchecked and need not be caught. But, the compiler requires that you must either catch a checked exception(IOException is a classic one) or declare to throw it.
This link might help : Java Language Specification
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thread.sleep might throw an InterruptedException if another thread calls interrupt method while it's sleeping. The compiler knows this and is basically asking you to catch that potential Exception as it's a checked exception. Here's the docs:


java.lang.InterruptedException
Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity. Occasionally a method may wish to test whether the current thread has been interrupted, and if so, to immediately throw this exception. The following code can be used to achieve this effect:
if (Thread.interrupted()) // Clears interrupted status!
throw new InterruptedException();

 
Rahul Nair
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

If i declare catch(Exception ex){} after try block it compiles and run fine..

hi Vidhya, As per your answer..
I declare the exception in main (main throws exception).. So for checked exception the handle or declare rule is satisfied..so i think in that case it has to compile without error...

Hi nico,
i agree that sleep() method throws Interrupted exception..But the Issue is handling of the exception..

Instead of caught in pthread class when it returns to the caller..
I declare main throws exception..
If i change it to main throws InterruptedException then also it doesn't work..

???
 
Rahul Nair
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can AnyOne Tell me,
What is wrong with above code?
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

public void run()
{
System.out.println("Running Thread...");
for(int i=0;i<100;i++)
{
if(i%10 != 0)
{
try{
out.println("Pausing. . .");
Thread.sleep(10);
out.println("I is :"+i);
}
finally{
System.out.println("In Finally Block. . . ");
}
}
}
}



The run method is an implemented method from the Runnable Interface which violates us from declaring an exception like (run() throws InterruptedException)so it has to be caught after the try block it self . I think there is no other go ..!

Dude i may not be correct but i tried my best to come up with an explanation .Rectify if am wrong
 
Rahul Nair
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Sid Robin
Thanks for Reply..

I got it.
Your Answer is correct...
we don't write run throws Exception...

BUT IF, i change my access modifier of class pthread to public
i.e. instead of
class Pthread implements Runnable
i write
public class Pthread implements Runnable

then it wouldn't compile..It shows follwoing error message,

"ThreadPause.java:4: class Pthread is public, should be declared in a file named pthread.java..."

But my main() in ThreadPause class so i gave file name as ThrreadPause.java

public is accessible anywhere...

So, why it shows an above error message ???
 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can only have one public class in one .java file: the one which has main() defined. However, note that it is ok to have no public classes in a file. In your case, you can have ThreadPause declared as public or no modifier, but main() itself HAS TO BE PUBLIC.

But, PThread cannot be public under any circumstance.
 
We're all out of roofs. But we still have tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic