• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we have nested catch?

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we have nested catch as we have for try???
 
Henry Wong
author
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Henry
 
Carlo Moore
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That structure is only illegal because the try block never throws an IOException, so you cannot catch it.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can always throw the exception yourself if trying it out is all you want to do.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic