Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Networking TechnologyRSS Feeds

Program which creates an Array of character. Make one function with one argument as a character and that function throw a user defined exception

Posted By: Alyssa Campbell     Category: Java     Views: 8953

A program which creates an Array of character. Make one function with one argument as a character and that function throw a user defined exception if the character that u have passed is digit.

Code for Program which creates an Array of character. Make one function with one argument as a character and that function throw a user defined exception in Java

class MyException extends Exception
{
       privatechar detail;
       
       MyException(char c) 
       {
              detail=c;
       }
       
       public String toString()
       {
              return"MyException["+detail+"]";
       }
}

class MyExc
{
        staticvoid myfunc(char ch) throws MyException
        {
               System.out.println("Called myfunc("+ch+")");
               if(ch=='0' || ch=='1' || ch=='2' || ch=='3' || ch=='4' || ch=='5'
                   || ch=='6' || ch=='7' || ch=='8' || ch=='9')
                      thrownew MyException(ch);
                System.out.println("Normal Exit...");
        }
       
         publicstaticvoid main(String args[])
         {
                try
                {
                       myfunc('g');
                       myfunc('7');
                }
                catch(MyException e)
                {
                       System.out.println("Caught "+e);               
                }
         }
}


/*
Output

Called myfunc(g)
Normal Exit...
Called myfunc(7)
Caught MyException[7]

*/
  
Share: 



Alyssa Campbell
Alyssa Campbell author of Program which creates an Array of character. Make one function with one argument as a character and that function throw a user defined exception is from Toronto, Canada.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!