Java Throwing Exceptions

Java Throwing Exceptions.

  • We can throw our own exceptions on:
    1. either instances of Exception,
    2. one of its existing subclasses, or
    3. our own specialized exception classes
    Our own created exception example:
    ...
    class SecurityException extends Exception {
     SecurityException() { super(); }
     SecurityException( String desc ) {
                super( desc );
       }
    }
    ...
    public void checkRead( String s ) {
       if ( new File(s).isAbsolute(  ) || (s.indexOf("..") != -1) )
         throw new SecurityException("Access to file : "+ s +" denied.");
    }
    ...
  • In this case, we throw in our method an SecurityException, which we earlier have created.
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.