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) 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) 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) What is the purpose of JDBC?

A) -> Java application can do any task by making a method (function) call. ->java method calls are not understandable to database management system. They can understand only SQL statements. ->SQL statements can’t be directly used in a java application. Java compiler complains. ->therefore, we say that java environment and database environment are heterogeneous to each other. Purpose of JDBC: ->for any kind of java application to communicate with any kind of database (management system) in a standard manner, JDBC is used.