• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

empty try block

 
chetana kumar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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?
 
Surendra Kumar
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have that, provided that the exception is some unchecked exception.
 
Brian Mozhdehi
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • 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.
 
Stary Kapec
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • 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?
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • 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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It wont throw a compiler issue, i.e. it will compile.

Wrong. This code produces a compiler error:
 
Henry Wong
author
Marshal
Pie
Posts: 21005
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic