explain me with a code snippet about the generation and
handling of null point exceptions.
Answer / g.arun
class Test
{
public static void main(String[] args)
{
try
{
int a[] = null;
System.out.println(a.length);
}
catch (NullPointerException n)
{
System.out.println(n);
}
}
}
U can try this by changing NullPointerException in catch
block as catch(Exception e){ System.out.println(n);}
and Using throw it is
class Test
{
public static void main(String[] args)
{
int a[] = null;
System.out.println(a.length);
NullPointerException n = new NullPointerException();
throw n;
}
}
Using Throws
class Test
{
public static void main(String[] args) throws
Exception
{
int a[] = null;
System.out.println(a.length);
}
}
That's all friends hope this one helps you
| Is This Answer Correct ? | 0 Yes | 0 No |
What is the difference between a local variable and an instance variable?
What is System class and its purpose?
What is an class?
Can a boolean be null java?
What are different types of states exist for a thread?
what is instanceof operator used in java?
Can we have multiple public classes in a java source file?
What is local variable and instance variable?
Can constructor return value?
extending thread class or implementing runnable interface. Which is better? : Java thread
What is final class?
When do we use synchronized methods in java?