Trace-run time error using try catch keywords

In this blog, we will see 2 programs. 1st program is not handled and other is handled run time error. Using of these 2 programs we can understand trace-run time error.

1st – program [Trace-run time error]

class Exc5

{

            static void method1(int x,int y)

            {

                        int div = x/y;  

                        System.out.println(“Div = ” + div);

            }

            public static void main(String args[])

            {

                        method1(15,0);          // Call to static method1()

            }

}          

Output:

Exception in thread “main” java.lang.ArithmeticException: / by zero

At Exc5.method1 (Exc5.java:11)

At Exc5.main (Exc5.java:19)

 

In this program, error in method1 (int,int) method, but we can see trace error in output screen.

 

2nd Program [Trace-run time error]

class Exc6

{

            static void method1(int x,int y)

            {

                        int div = x/y;  

                        System.out.println(“Div = ” + div);

            }

            public static void main(String args[])

            {

                        try

                        {

                                    method1(15,0);          // Call to static method1()

                        }

                        catch(ArithmeticException ex)

                        {

                                    System.out.println(ex);

                        }          

            }

}

 

Output -2

Java.lang.ArithmeticException: / by zero

 

In this program, we can manage trace error using 1 try catch block.

 

Video available on YouTube now.

 


[sbscrbr_form]

If you have query about this blog, you can directly post comment or subscribe on website.


Earn money online

345total visits,1visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Comments are closed.