how can u handle run time exception in java? explain with
brief explanation with examples?

Answers were Sorted based on User's Feedback



how can u handle run time exception in java? explain with brief explanation with examples?..

Answer / pavithra

public class ExceptionalClass {
public void method1() throws CheckedException {
// ... some code
throw new CheckedException( "something bad
happened" );
}
public void method2( String arg ) {
if( arg == null ) {
throw new NullPointerException( "arg passed to
method2 is null" );
}
}
public void method3() throws CheckedException {
method1();
}
}

Contrast method1() with method2(). When you
make a call to method2(), you don't have to do so within a
try/catch block. Since you do not have to catch the
exception, the exception is said to be unchecked; it is a
runtime exception. A method that throws a runtime exception
need not declare the exception in its signature.

Is This Answer Correct ?    9 Yes 3 No

how can u handle run time exception in java? explain with brief explanation with examples?..

Answer / pavithra

Runtime exceptions represent problems that are detected by
the runtime system. This includes arithmetic exceptions
(such as when dividing by zero), pointer exceptions (such
as trying to access an object through a null reference),
and indexing exceptions (such as attempting to access an
array element through an index that is too large or too
small).

Runtime exceptions can occur anywhere in a program and in a
typical program can be very numerous. Typically, the cost
of checking for runtime exceptions exceeds the benefit of
catching or specifying them. Thus the compiler does not
require that you catch or specify runtime exceptions,
although you can.

Is This Answer Correct ?    5 Yes 8 No

Post New Answer

More Core Java Interview Questions

How to sort a vector elements that contains the user define class object? (Note: If Suppose consider, A Student class contain two data members. They are String studentName and int rollNo. I am creating Four objects for this class, each object contains students details like name and roll no. Now i am storing that objects in vector and if i retiving the elements from the vector means then it should be display in sorting order)

3 Answers   ProdEx Technologies,


What is exception in java?

0 Answers   Cyient,


How we can run a jar file through command prompt in java?

0 Answers  


What is OOP Language?

0 Answers   Atos Origin,


Explain inner classes ?

0 Answers  


Name few "optional" classes introduced with java 8 ?

0 Answers  


What is a get method?

0 Answers  


Explain an intermediate language?

0 Answers  


What are the main uses of this keyword?

0 Answers  


What is immutable in java?

0 Answers  


What are different exception types exceptions available in java ?

0 Answers  


how to identify duplicate values in arraylist

2 Answers   TCS,


Categories