Skip to main content

Q) Example program an user defined exception implementation?

Q) Example program an user defined exception implementation?
A)


class votingException extends Exception
{
      
}//user defined, checked Exception

class voting
{
       public static void isEligibletovote(int age) throws votingException
       {
              if (age<19)
                     throw new votingException();
              System.out.println("can cast vote");
       }
}
class votingapplication {
public static void main(String[] args) {
       try
       {
              int age=Integer.parseInt(args[0]);
              voting.isEligibletovote(age);
       }
       catch(Exception e)
       {
              System.out.println(e);
              if(e instanceof votingException)
                     System.out.println("invalidage. can't vote");
              if( e instanceofNumberFormatException)
                     System.out.println("please supply again digits only");
       }
/*only one catch block. It is not advisable because more no.of conditions reduces the performance.*/

 /*catch(votingException e)
  {
    e.printStackTrace();
    System.out.println("invalid age. can't vote");
  }
  catch(NumberFromatException e)
  {
    System.out.pritnln(e.getMessage());
    System.out.println("please supply age in digits only");
 }*/
}
}

Output:
java.lang.ArrayIndexOutOfBoundsException: 0

Note:
->Whenever multiple exceptions i.e. more than one kind of exception raising chance is there in try block, to handle each exception specifically, we go for multiple catch blocks.
->even though multiple catch blocks are implemented, only one catch block is executed.
->when we specify multiple catch blocks, super class executed catch blocks should not precedence the sub class exception catch block.

Unreadable code is a syntactical error in java”

Ex:
try
{ }
catch(Exception e)
{ }
catch(NumberFormateException e)
{ }

->Here syntactical exception is raised. The super class exception(i.e. Exception) is precedence to the its child class exception (i.e. NumberFormateException).

try
{ }
catch(NumberFormateException e)
{ }
catch (Exception e)
{ }

->Here error is not raised.

Additional information:
->getMe() it returns a string not location information.                                                                                                                                                                                                                                                
It returns the error description but not returns where it is raised. i.e. line number (it is location information).
->1) type of exception
    2) Error description
    3) Location is also displayed with e.printStackTrace()
->when directly priting the exception reference i.e. when we display System.out.println(e);
Then it display:
Type of exception
Error description, but it not displays location of exception.

Comments

Popular posts from this blog

Q) What is JDBC driver? What is its role in java database connection?

A) ->A translation software written in java according o JDBC specification is nothing but “JDBC driver”. -> JDBC driver implements JDBC API. Use single API. JDBC driver role: They are 4 roles   1)       Establishing connection between JDBC client and database server. 2)       Receiving JDBC method calls made by JDBC client while performing database (CRUD) operations. 3)       Translating java method calls into DBMS understandable calls and forwarding them to database server. 4)       Receiving the results from database server. Translating them into java format and handling over the same to the JDBC client.

Q) What is ODBC?

A) ->open database connectivity is a technology from MS that enables any language program (other than java) to communication with database Management System. ->in java program ODBC is directly not allowed for the fallowing reasons 1) OS dependency 2) Procedural oriented approach 3) Security thread

Q) Explain about nested classes?

A) ->defining a class with in another class is known as nesting of the class. Ex: class A {        class B        {                      } //nested class, and inner class.because it is not static class. } //enclosing class or outer class. ->static class is declared in inside an another class then it does not become an inner class. Note: non-static nested class is known as “inner class”. In the above example class B is nested but not an inner class. Ex: class D {        static c        {                      } //nested class, but not an inner class , because of static class. } //outer class. ->we have three different kinds of inner classes. 1) Member inner class 2) Meth...