aspose file tools*
The moose likes Java in General and the fly likes Can we have nested catch? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can we have nested catch?" Watch "Can we have nested catch?" New topic
Author

Can we have nested catch?

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Can we have nested catch as we have for try???


http://www.lifesbizzare.blogspot.com || OCJP:81%
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18981
    
  40

Vishal Hegde wrote:Can we have nested catch as we have for try???


The try / catch syntax works together. So, if you have a "catch" nested within another catch statement, you need the "try" nested in the same catch block as well.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18981
    
  40


BTW, you could have also just tried it -- to see if it works.

Henry
Carlo Moore
Greenhorn

Joined: Aug 02, 2005
Posts: 27
You can definitely nest try / catches but be aware that if your inner catch catches an exception which is the outer catches superclass, the outer catch becomes redundant. IE...

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Henry Wong wrote:
BTW, you could have also just tried it -- to see if it works.

Henry

Hi,

I tried it was getting compilation error was just trying


Got compilation error just also wanted to get the same thing verified here too
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Vishal Hegde wrote:
Henry Wong wrote:
BTW, you could have also just tried it -- to see if it works.

Henry

Hi,

I tried it was getting compilation error was just trying



Yes, that structure is legal.

Got compilation error


You've been around here enough that you now by now that ItDoesntWorkIsUseless(⇐click) and if you want help you need to TellTheDetails.(⇐click)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

That structure is only illegal because the try block never throws an IOException, so you cannot catch it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rob Spoor wrote:That structure is only illegal because the try block never throws an IOException, so you cannot catch it.


Right. I meant the "structure" is legal in that you can have those pieces arranged that way relative to each other. I didn't mean to imply that it should compile as-is.
Carlo Moore
Greenhorn

Joined: Aug 02, 2005
Posts: 27
You can always throw the exception yourself if trying it out is all you want to do.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we have nested catch?