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) Retrieving data from database?

A)   ->to retrieve the data from database we need to submit SELECT statement from JDBC application. -> executeQuery() method of statement object is used for this purpose. This method has the fallowing ResultSet executeQuery(String sql) throws SQLException Ex: ResultSet rs = st.executeQuery(“SELECT * FROM ACCOUNT”); ->objectoriented representation of tables formate data is called ResultSet object. Ao the driver created one object i.e. ResultSet object. ->object orientation representation of a table of records returned from db is nothing but “ ResultSet ” object.  

Q) Explain repeated deletion of records.

A) //RepeatedDeletion.java import java.util.Scanner; import java.sql.*; class RepeatedDeletion {        public static void main(String[] args) {               Connection con= DriverManager. getConnection ( "jdbc:odbc:prince" , "scott" , "tiger" );               PreparedStatement ps=con.PreparedStatement( "DELETE FROM ACCOUNT WHERE ACCNO=?" );               Scanner s= new Scanner(System. in );               while ( true )               {                      System. out .println( "Enter accno : " );      ...

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.