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?

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

Can a class have an interface?

554


What is number data type?

532


What do you know about the garbage collector?

613


Implement two stacks using a single array.

574


Can we extend singleton class?

527






Explain polymorphism citing an example.

592


Is java developer a good career?

555


How do you allocate memory to object?

518


Explain what access modifiers can be used for variables?

593


Explain different ways of creating a thread. Which one would you prefer and why?

577


Where pragma is used?

600


Explain heap sort?

700


How to reverse string in java?

617


What is the dot operator?

572


What is the difference between inner class and nested class?

552