• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Problem with Exception

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

For the following code, it is giving me the error (while compiling)

class test{

public void f(){
try {throw new Exception();
}
finally {
System.out.println("CAUGHT1");
}
}

public static void main(String args[]){
test t = new test();
try {t.f();}
catch (Exception e) {
System.out.println("CAUGHT0");
}
}
}


ERROR****************
test.java:4: unreported exception java.lang.Exception; must be caught or declare
d to be thrown
try {throw new Exception();
^
1 error

I'm not able to understand the reason for compilation error. In try block I'm throwing an exception, and in the main call it is caught, so why am i supposed to declare the exception/ provide a catch for it, when already I'm catching it in main and providing a finally for it.

Please help,

Regards,
Gitesh
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
in your test class,


here in f() method , throws ne Exception .So , you should catch that exception or you can specify like this.


This will help you .
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Either you have to catch an exception OR you need to have a "throws" clause in the method where the exception is thrown.

Since you don't follow both of these rules in your f() method, the compiler is very happy to throw an exception at compile time.

HtH.
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See you have witten a method which is meant to be called form many places(depending upon the scope). Now what compiler can do is check you, where you are throwing the checked exception and report it there and secondly the user(except from you) of your method wont be able to know that he has to catch the checked exception untill and unless :
you use the throws clause in your method

Therefore never use the throw clause outside the scope of try-with-catch block (catching that or paren exception to that) and method which doesn't throw that or parent to that class.

I hope you'll be able to understand this now.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all, I got my Fundas cleared now..
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic