Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

try,catch

 
Mukul Vashishtha
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can we print wwwwwwww(inside system.out.println)
because when it get exception it jumps to catch.
class Account{
public static void main(String[] args)
{String accn=null;
try{
int i = 1%0;
System.out.println("wwwwwww");
}
catch(Exception e){

}
}}
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. The other line in your program will throw a java.lang.ArithmeticException. What are you trying to achieve? Do you want to do something regardless of exceptions? If so have you come across finally blocks?
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isint that the point of a try catch, that if an exception is found in the try part it stops executing at the point of the exception, ignoring any code after the exception and "jumping" to the catch block.

So really there is no way to execute code in tehtry block after the exception occurs.

However if you had code that you always wanted to execute no matter if there is an exception or not, you could try putting it in to a finally block, you know:
 
Michael Jennings
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, if you really want to do it.

bad way :
change, int i = 1%0
to, float i = 1%0.0

good way :
put the println in "finally" block
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi yellowjersey -

We've found that when people use their real name as their display name things stay a lot friendlier, so I'd ask you to update your display name to match the JavaRanch policy.

Thanks, and welcome to the ranch.

Bert
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic