IllegalAccessException with throws

IllegalAccessException

In this blog, we will work on IllegalAccessException using throws keywords. This example is same as NullPointerException using throw keyword.

Using this example we can understand basic different between throws and throw.

 

Example on IllegalAccessException

class A

{

            void setMethod1() throws IllegalAccessException

            {          

                        // No need to try and catch block

                        throw new IllegalAccessException(“Error Message”);     

            }

}

class Exc9

{

            public static void main(String args[])

            {

                        A a1 = new A();

 

                        try

                        {

                                    a1.setMethod1();       // call in A Class           

                        }

                        catch(IllegalAccessException ex)

                        {

                                    System.out.println(“Main : ” + ex);

                        }

            }

}          

Output:

Main: java.lang.IllegalAccessExceptions : Error Message

 

In this example, we can see that throws keyword set with “setMethod1() throws IllegalAccessExceptions”. Using of this, we do not need any try catch block in setMethod1() function.

 

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

876total visits,7visits today


mayank

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

Related articles

Comments are closed.