You are on page 1of 5

Exceptions - Sub set of errors. Errors - any abnormal conditions. Type of errors compile time errors. * we can't trapped(Ignore).

; , a , system, integer Runtime errors. * we can trap by using the Exception handling. / by 0 , NumberFormat , array index

all run time errors r called Exceptions & base class of all exception - java.lang.Throwable java.lang - package , this is default import. Throwable - class - it has 2 sub classes. java.lang.Error * we can't handle. * end with Error NoSuchMethodError java.lang.Exception * we can handle. * end with Exception ArithmeticException

Adv / need of Exception handling - by using the exception handling, we can control the abnormal termination of program. java.lang.Error - it has 1- NoSuchMethodError - if method not found, like program without using p s v m 2- NoClassDefFoundError - if class def not found. java Haha 3- StackOverFlowError - if JVM stack is full - (memory problem ) class Stack { public static void main(String aa[]) { System.out.println("Vivek says, I m a good boy"); main(aa); // recursive main calling. } } in this program we have StackOverFlowError ___________________________________________________________________ java.lang.Exception - it has Exceptions Type CheckedException UnCheckedException * need to handle, else we have compile * no need to handle, but if any time error(must be in try catch or exception in Runtime then with throws) program terminate. UnCheckedException -

-----------------1- RuntimeException 1- ArithmeticException - / by 0 int a = Integer.parseInt(aa[0]); int x = 100 / a; pass 5 20 10 0 value of x x = 20 x = 5 x = 10 x = ArithmeticException

2- NumberFormatException - invalid number for type casting int a = Integer.parseInt(aa[0]); pass 5 20 10 haha value of a a = 5 a = 20 a = 10 a = NumberFormatException

3- IndexOutOfBoundsException - if array & String index not exist. i - ArrayIndexOutOfBoundsException - if array index not exist. int a = Integer.parseInt(aa[0]); pass 5 20 10 no argument value of a a = 5 a = 20 a = 10 a = ArrayIndexOutOfBoundsException

int a[] = new int[5]; - array index 0 to 4 a[26] = 10; - ArrayIndexOutOfBoundsException ii - StringIndexOutOfBoundsException - if String index not exist. String s = "Harsh"; char c = s.charAt(2); c = 'r'; - char at 2 index, start 0 to n char c = s.charAt(26); c = StringIndexOutOfBoundsException 4- NegativeArraySizeException - if array size -ve int a[] = new int[5]; - valid int a[] = new int[-5]; - NegativeArraySizeException 2- NullPointerException - if object is null. Major is project Development. String u = req.getParameter("t1"); // get user id parameter from request obj ect if parameter not passed by the user then u is null & u use in future then we have NullPointerException

CheckedException ---------------3- ClassNotFoundException - in JDBC programming. Class.forName("oracle.jdbc.driver.OracleDriver"); - for connectivity with oracle, but if oracle not installed in PC then we have ClassNotFoundException 4- SQLException - in JDBC programming - for any type of SQL related problem - like table not exist, number of cols less, col type mismatch , constraints prob, database read only. s.execute("insert into emp values(1001,'Vivek Sharma',32999,'20-Jan-1989')") ; 5- IOException - in Runtime input & file handling. 6- InterruptedException - in multi Threaded programming . 7- SecurityException - in Applet programming. 8- UnkownHostException - in Socket programming. if Remote PC not found. 9- RemoteException - in RMI programming, if any type of Remote problem. 10- NotBoundsException - in RMI programming, if RemoteServer object not found. 11- SerevletException - in Servlets programming 12- FileNotFoundException - if file not found for input. 13- StreamCurreptedException - if file currepted 14- InputMismatchException - if runtime input mismatch. add in jdk1.5 for Scanner class. 15- CreateException - in EJB programming. ____________________________________________________________________ how to handle try - check the code there is any Exception or not, if yes then handle in catch block. catch - find out the exception type. finally - at the end of try & catch block, this is optional throw - use with condition for genarate the new exception throws - use with method body for handling the exception ** one try block should have one catch or one finally block - must. package my; class Ex1 { public static void main(String aa[]) { int a = Integer.parseInt(aa[0]); // NumberFormat + ArrayIndex int x = 100 / a; // Arithmetic System.out.println("x = "+x);

System.out.println("Thanxxxxxxxxxxxxxxxxxxxxxxxxxx"); } } // javac -d . Ex1.java // java my.Ex1 5 // Thanx msg display only when there is no exception pass any number of command line argument, if they r numbers - add if String - concat pass 2 command line argument if they r float values then ans in float , if int ans in int java Sum 100.22 50.44 java Sum 100.22 50 java Sum 100 50 ans - Sum is = 150.66 ans - Sum is = 150.22 ans - Sum is = 150

--------------------------------------------------------------------Runtime inputs in java System.in.read(byte[]) DataInputStream / BufferdReader

* static input - has max limit. * Dynamic Input - No Max Limit. * byte mode - byte by byte. * Line Mode - read whole Line. * Type casting not supported. * type casting Supported - parseInt() input with read() method 1- to create an byte array byte b[] = new byte[10]; 2- pass in read method System.in.read(b) 3- convert into String String s = new String(b); 4- display System.out.println("Welcome "+s); 3 & 4 th step System.out.println("Welcome "+new String(b)); problem with read() method * * * * static input - has max limit. Type casting not supported. More Complex - 1 input , 4 lines , 10 inputs - 10 *4 = 40 Lines. finally no use.

---------------------------------------------------------------------input with DataInputStream DataInputStream r = new DataInputStream(System.in); String s = r.readLine(); - read whole line * Adv - Line Mode - Dynamic Input * problem - work with warnings. -----------------------------------------------------------------I/O Operations Byte Mode * old concept - part of java1 * support 256 charset only (English only) * work with warnings. * end with Stream char mode * new concept - add of java2 * support 512 charset also (National charset - unicodes) * work without warnings. * end with Reader/Writer

-------------------------------------------------------------------input with BufferedReader char mode byte to char byte mode BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); String s = r.readLine(); - read whole line Adv - work without warnings. problems * no method for input single word/char only. * no method for primitive data input, like int, long, float, double, byte for that we need to type casting. * More complex * finally - no use ___________________________________________________________________ new concept add in jdk1.5 for Runtime input - with Scanner class Scanner r = new Scanner(System.in); int nextInt() - read/input int value float nextFloat() - read/input float value long nextLong() - read/input long value double nextDouble() - read/input double value byte nextByte() - read/input byte value String next() - read single word only. String nextLine() - read whole line.

You might also like