Java: How to Know in which line Java Exception Occurred?

There is  a very simple way to know where and which line exception occurs in your java code, and it is  very useful tool for diagnosing an Exception.printStackTrace()  is that simple tool which will help you to diagnose your code in an easy way

It  will tell you what happened  and  where (in which line )  in the code this exception  happened

 printStackTrace()  is  a method of the Throwable class. All Exceptions are a subclass of that class.

It’s a great debugging tool.

Here is the code example with output to show the function of  printStackTrace()

import java.io.*;

class numerator1

{

static String input;

static int intInput;

                        public void myNumerator()

                        {

                                                            try

                                                            {

            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

            System.out.println(“\n”);

                                                            System.out.println(“Enter the Numeric:”);

                                                            System.out.println(“\n”);

                                                            input=br.readLine();

                                                            intInput=Integer.parseInt(input);

                                                            System.out.println(“\n”);

                                                            System.out.println(“You  have Entered:”+intInput);

                                                            System.out.println(“\n”);

                                                            intInput=intInput/0;

      //It will cause an error as I have divided the input number by zero(0)

      //java.lang.ArithmeticException: / by zero  (Check the Output)        

                                                            System.out.println(“\n”);

                                                            }

                                                            catch(Exception e)

                                                            {

                                                            //System.out.println(“Error:”+e.getMessage());

                                    Exception newEx = new Exception(“Error at:”+new java.util.Date()+””,e);

                                    newEx.printStackTrace();

                                                            }

                        }

public static void main(String args[])

{

numerator1 x=new numerator1();

x.myNumerator();

}

}OUTPUT:

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 306 other followers

%d bloggers like this: