dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Sep 17, 2003

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Determine If an Error Has Occurred During Object Construction

This can be achieved in Java by throwing an exception from your constructor. Since constructors cannot return a value, this is the only way to perform error handling.

     Class MyCLass{
          private int x;
          File file;
         MyClass() throws MyException{
         x = 10;
         try{
         file = new File(“c:\\myFile.txt”);
         }
         catch(Exception e)
         {
          throw new MyException();
         }
         /
         /
       }
     }
When you use this class, insert the code of the constructor in a try-catch block and catch the exception:

    try{
        MyClass mc = new MyClass();
       }
    catch(MyException e)
      {
       System.out.println(“Could not create an object of the class
MyClass”);
      }
Sashikanth Maddali
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date