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 : " );      ...

Introduction

1) What is not JDBC? 2) What is the purpose of JDBC? 3) What is ODBC? 4) What is JDBC? 5) What is JDBC architecture? 6) What is JDBC client? What are its responsibilities? 7) What is JDBC API?  8) What is driver manager? 9) What is JDBC driver? What is its role in java database connection? 10) What is database server?

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...