How can we handle runtime exceptions? write one sample
program? Can we write runtime exceptions beside the throws
key word? if yes write sample program?



How can we handle runtime exceptions? write one sample program? Can we write runtime exceptions be..

Answer / amit2009mca

there is no need to handle a runtime exception,
but acording to question
there are two types through which we could handle the
exception one is decalre the exception and other try catch
block.
so for this i'll show you the customize way for this.

class AmitSingh extends RuntimeException
{
}

class Amit
{
int number;
Amit(int number)
{
this.number = number;
}

public void method1()
{
try
{
if(number>5)
throw new AmitSingh();
}
catch(AmitSingh e)
{
System.out.println(e.printStackTrace());
}
public static void main(String []args)
{
Amit a = new Amit(20);
a.method1();
}
}

but i don't think there is need to this because jvm is
responsible for all that thanks AMIT SINGH09

Is This Answer Correct ?    2 Yes 1 No

Post New Answer

More Core Java Interview Questions

What are static initalizers in java ?

0 Answers  


Is char a data type in java?

0 Answers  


How we can make copy of a java object?

0 Answers  


What is the top class of AWT event hierarchy?

5 Answers  


What is the difference between sleep and wait in java?

0 Answers  






I want to re-reach and use an object once it has been garbage collected. How it's possible?

0 Answers  


What is extension method in java?

0 Answers  


Difference between abstract class and Interfaces?

7 Answers   CTS, iFlex, PA Consulting, Sai Softech, Value Chain,


Can we sort a map in java?

0 Answers  


what is polymorphism?

5 Answers   Zensar,


What is the difference between DataInputStream and BufferedReader

2 Answers   Hexagon,


How 'java' got its name and what it stands for?

11 Answers   Wipro,


Categories