Adv Java
Programming

1. What Is the Java Technology 2. Points to keep in mind for pyramid. 3. Java Pyramid Program on type 1 4. Java Pyramid Program on type 2 5. Java Pyramid Programs on Type 3 6. Java Pyramid Programs on type 4 7. Java Pyramid Programs on type 5 8. Classes and Objects 9. Recursive functions 10. Constructors in Java 11. Method Overloading 12. Constructor Overloading 13. Inheritance in Java 14. Interface in Java 15. Method Overriding and Hiding Method 16. The final keyword 17. Exception Handling 18. Properties, System & Runtime Class in Java 19. Building Java GUI 20. Layout Managers 21. Menu Bar 22. Applets 23. Threading 24. Java Netwoking 25. Programs for Science and Engineer 26. Prg to demonstrate pre and post increment 27. Prg to convert Farenheit to Celsius temperature 28. Prg to convert kilogram to different units 29. Prg to calculate surface area and volume of sphere 30. Prg to calculate frequency 31. Prg to determine co-ordinate points 32. Prg to determine Equilateral, Isosceles or Scalene triangle 33. Prg to demonstrate conditional operator 34. Prg to calculate Final and initial interest amount 35. Prg to separate entered number 36. Prg to examine while loops and conditions 37. Prg to calculate number of year 38. Prg to find Armstrong number or not 39. Prg to find all Armstrong number 40. Prg to find limited Armstrong number 41. Prg to find Armstrong number using user defined function 42. Prg to find frequency of student 43. Prg to find maximum and minimum number 44. Prg to print number triangle 45. Prg to print vertical number triangle 46. Prg to print number triangle 47. Prg to print binary right angle triangle 48. Prg to print * triangle 49. Prg to to print rhombus 50. Prg to print entered number triangle 51. Prg to to find nCr and nPr 52. Prg to enter an element in array 53. Prg to sort number using array 54. Prg to find the position of element in array 55. Prg to find the position of element in array using boolean flag 56. Prg to add one extra element in an array 57. Prg to find the equation of line 58. Prg to find the common element from two array 59. Prg to find the highest scorer 60. Prg to create a square of number 61. Prg to create Pascal triangle 62. Prg to subtraction, multiplication and division using methods and classes 63. Prg to determine salary grade 64. Prg for complex number 65. Prg to calculate area 66. Prg for Greatest Common Divisior 67. Prg for displaying studenta mark sheet 68. Prg for demonstrating inheritance 69. Prg to calculate area of triangle 70. Prg for calculating volume of sphere or hemisphere 71. Prg for demonstrate classes 72. Prg for employee details 73. Prg for demonstrating super and base class 74. Prg for Bank Account 75. Prg for professor and student details 76. Prg for Addition, Multiplication and finding Transpose of two matrix 77. Prg for counting number of words in a string 78. Prg to count number of vowels and consonants in a string 79. Prg to fine number of upper case, lower case, white spaces and digit in a string 80. Prg to fine number of upper case, lower case, white spaces and digit in a string without inbuilt function 81. Prg to arrange a string in alphabetic order 82. Prg to count the number of character occurrence in a string 83. Prg to find the string length and character position 84. Prg for converting first character of word into upper case 85. Prg for demonstrating vectors for manipulating records 86. Prg for manupulting items record using vectors 87. Prg for bank transaction 88. Prg for counting number of days between any two different date 89. Prg for exception handling 90. Prg for throwing exception 91. Prg for exception 92. Prg to throw exception when number is invalid 93. Prg for renaming the current thread 94. Prg for demonstrating strat method of thread 95. Prg to demonstrate sleep and and join method of thread 96. Prg to demonstrate yield method of thread 97. Prg for demonstrating thread in class inheritance 98. Threading program using runnable interface 99. Thread program using sleep 100. Java Applet program for creating fancy round eye 101. Java Applet program for creating fancy face 102. Java Applet program for drawing string 103. Java Applet program for creating fancy face 104. Java Applet program for displaying fancy fonts 105. Java Applet program for drawing and filling rectangle 106. Java Applet program for creating house 107. Java Applet program to show lifecycle of applet 108. Java Applet program for creating polygon 109. Java Applet program for creating rectangle 110. Java Applet program for drawing line 111. Java Applet program for getting applet width and height 112. Interview Question & Answer part 1 113. Interview Question & Answer part 2 114. Interview Question & Answer part 3 115. Interview Question & Answer part 4 116. Interview Question & Answer part 5 117. Interview Question & Answer part 6 118. Interview Question & Answer part 7 119. Interview Question & Answer part 8 120. Interview Question & Answer part 9 121. Interview Question & Answer part 10 122. Interview Question & Answer part 11
Pr.Pg Next Pg

Exception Handling tutorials

  • An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.

  • The process of responding to the occurrence, during computation, of exceptions anomalous or exceptional events requiring special processing often changing the normal flow of program execution.

  • When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred.

  • Creating an exception object and handing it to the runtime system is called throwing an exception.

  • Conditions that can readily occur in a correct program are checked exceptions. These are represented by the Exception class.

  • Severe problems that normally are treated as fatal or situations that probably reflect program bugs are unchecked exceptions.

Different between Error and Exception

Error

Exception

An error is an irrecoverable condition occurring at runtime.

Exceptions are conditions that occur because of bad input.

Such as Out Of Memory error. These JVM errors and you can not repair them at runtime.

FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference.

Though error can be caught in catch block but the execution of application will come to a halt and is not recoverable.

In most of the cases it is possible to recover from an exception probably by giving user a feedback for entering proper values etc.

Error in Java are unchecked

Exception in Java are checked and unchecked.

 

 

Example: Java program to demonstrating an Exception in adding two numbers

 Sam Madam

//Program name ExceptionDemo.java

//Java program to demonstrating an Exception in adding two numbers

public class ExceptionDemo

{

public static void main(String[] args)

{

int num1,num2,sum;

System.out.print("Enter Num1 : ");

Scanner scan =new Scanner(System.in);

num1=scan.nextInt();

System.out.print("Enter Num2 : ");

num2=scan.nextInt();

sum=num1+num2;

System.out.print("Sum of Num1 and Num2 : "+sum);

}

}

Output

Enter Num1 : 10

Enter Num2 : 15

Sum of Num1 and Num2 : 25

 

Enter Num1 : 10

Enter Num2 : f

Exception in thread "main" java.util.InputMismatchException

at java.util.Scanner.throwFor(Scanner.java:840)

at java.util.Scanner.next(Scanner.java:1461)

at java.util.Scanner.nextInt(Scanner.java:2091)

at java.util.Scanner.nextInt(Scanner.java:2050)

at ExceptionDemo.main(ExceptionDemo.java:11)

 

Enter Num1 : 10

Enter Num2 : 10.5

Exception in thread "main" java.util.InputMismatchException

at java.util.Scanner.throwFor(Scanner.java:840)

at java.util.Scanner.next(Scanner.java:1461)

at java.util.Scanner.nextInt(Scanner.java:2091)

at java.util.Scanner.nextInt(Scanner.java:2050)

at ExceptionDemo.main(ExceptionDemo.java:11)

  • Note: In above program if you enter proper integer number then program would sum up properly but if you try to enter any character or floating number then it will throw an exception as “Exception in thread "main" java.util.InputMismatchException” and program will terminate.

  • To avoid this abnormal termination you can use try…catch.. finally block for handling exceptions.

 

Exception Handling using try.. catch and finally block

  1. try

    • The first step in constructing an exception handler is to enclose the code that might throw an exception within a try block.

    • In general, a try block looks like the following:

try

{

// code

}

    • If an exception occurs within the try block, that exception is handled by an exception handler associated with it.

    • To associate an exception handler with a try block, you must put a catch block after it.

  1. Catch

    • At least one catch is required for the try-catch. finally is not required but that has it own uses.

    • When an exception is throw and a try-catch is found, Java will look through all the catch statments to see what they actually will catch. This goes in the order that they are written.

    • In general, a try..catch block looks like the following:

try

{

// Code

}

catch(Exceptiontype name)

{

}

catch(Exceptiontype name)

{

}

    • Each catch block is an exception handler and handles the type of exception indicated by its argument.

    • The argument type, ExceptionType, declares the type of exception that the handler can handle and must be the name of a class that inherits from the Throwable class. The handler can refer to the exception with name.

    • The catch block contains code that is executed if and when the exception handler is invoked. The runtime system invokes the exception handler when the handler is the first one in the call stack whose ExceptionType matches the type of the exception thrown.

    • The system considers it a match if the thrown object can legally be assigned to the exception handler's argument.


  1. finally

    • The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.

    • But finally is useful for more than just exception handlingit allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break.

    • Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

    • In general, a try..catch..finally block looks like the following:

try

{

// Code

}

catch(Exceptiontype name)

{

}

catch(Exceptiontype name)

}

}

..

..

finally

{

//finally code.

}

 


 

Example: Java program to demonstrating an Exception Handling in adding two numbers

 Sam Madam

//Program name ExceptionDemo.java

//Java program to demonstrating an Exception Handling in adding two numbers

import java.util.Scanner;

public class ExceptionDemo

{

public static void main(String[] args)

{

int num1,num2,sum;

try

{

System.out.print("Enter Num1 : ");

Scanner scan =new Scanner(System.in);

num1=scan.nextInt();

System.out.print("Enter Num2 : ");

num2=scan.nextInt();

sum=num1+num2;

System.out.println("Sum of Num1 and Num2 : "+sum);

}

catch(Exception ex)

{

System.out.println("Exception Occur: "+ex);

}

finally

{

System.out.println("Finally program ends..");

}

}

}

Output

Enter Num1 : 10

Enter Num2 : 15

Sum of Num1 and Num2 : 25

Finally program ends..

 

Enter Num1 : 10

Enter Num2 : 10.5

Exception Occur: java.util.InputMismatchException

Finally program ends..

Pr.Pg border                                              Next Pg