• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Resume of program

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

Following is scenario

1 public amethod()
2 {
3 int j=45/0;// Here code will throw runtime exception
4 system.out.println("Hi");
5 }

I want to execute code of line 4 any way .

please help me how we can resume program after throwing an exception on line 3 .

Thanks
 
Sheriff
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Catch the exception.
 
sandeepkumar patil
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can code like this
try{
public amethod()
{
int j=45/0;// Here code will throw runtime exception
system.out.println("Hi");
}
} catch(Exception ex)
{
}

but I cant put try- catch like this

public amethod()
{
try{

int j=45/0;// Here code will throw runtime exception
catch(Exception ex)
{
}

system.out.println("Hi");
}
}
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please put your code between the code tags.

Your code throws a ArithmeticException which is of type RuntimeException and hence unchecked.

so, you need to catch either of the two.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
finally can be used even if you aren't catching an exception
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:finally can be used even if you aren't catching an exception



Which is a better approach since the sysout is not an exception handling and should be executed whether or not an exception occurs.
 
I AM MIGHTY! Especially when I hold this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic