the use of try and ffinally keyword

Answer Posted / xavier

try and finally are keywords associated with exception
handling(run time errors)
try block provides two benefits it allows you to fix the error.
it prevents program from automatically terminating
e.g.
class Exception_handling
{
public static void main(String args[]){
try{
int d=0;
int x= 42/d;
System.out.println("this will not be printed");
}
catch(Exception e)(
System.out.println("this will be printed");
}
}
}
you might observe that in above code the line after int
x=42/d is not printed but there are certain operations that
you need to perform before proceeding further and finally
would serve this purpose

class Exception_handling
{
public static void main(String args[]){
try{
int d=0;
int x= 42/d;
System.out.println("this will not be printed");
}
catch(Exception e)(
System.out.println("this will be printed");
}
finally{
system.out.println("this will be printed");
}
}
}

Is This Answer Correct ?    10 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How primary key is implemented in Oracle?

2143


What class is the top of the awt event hierarchy?

819


What is the argument type of a programs main() method?

803


What is the map interface?

834


What is a session? Can you share a session object between different theads?

761


AS a developer will u create a data source in connection pool? If so how will u do that, how to access the object from connection pool using IRAD tool?

1886


What do you know about seam?

779


What is the difference between java class and bean?

789


Where can I ask questions and make suggestions about seam?

826


Name three subclasses of the component class?

837


What are local interfaces? Describe.

895


What are transaction attributes?

826


What is the difference between the string and stringbuffer classes?

782


Can I have an action without a form?

818


Whats new with the stop(), suspend() and resume() methods in jdk 1.2?

781