• 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

empty try block

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what happens when you have a try block without any code folloewd by a catch block that catches some exception?
 
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can have that, provided that the exception is some unchecked exception.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing happens. The catch block will only catch an exception thrown in the Try block. If nothing happens in the try block, then no exception could ever possibly be thrown and therefore whatever is in the catch block would never be executed.

It wont throw a compiler issue, i.e. it will compile. But its pointless to add such a thing to any application.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You can have that, provided that the exception is some unchecked exception.





e and t exceptions are also allowed. Are they also considered as checked exceptions?
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those classes which extend Exception is checked only with the exception of java.lang.RuntimeException class.

Al other is unchecked. e.g., Throwable, Error, Exception all are unchecked.

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

It wont throw a compiler issue, i.e. it will compile.


Wrong. This code produces a compiler error:
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

e and t exceptions are also allowed. Are they also considered as checked exceptions?



The compiler will not complain about any unchecked exception -- or any throwable class that an unchecked exception is an instance of. There are unchecked exceptions that are instance of the Exception and Throwable classes, hence, it will not complain.

Henry
 
Stary Kapec
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Throwable, Error, Exception all are unchecked.





But even though Exception is not checked, compiler still requires it to be checked :-) So why not to call it checked? The same is true for Throwable.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How the compiler will know which exception object is thrown from foo() method. It could be an instance of IOException which is checked so compiler will force you to catch it or declare it by throws clause.

Naseem
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic