Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

try,catch

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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){

}
}}
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic