There are mainly two types of exceptions:
The sun microsystem says there are three types of exceptions:
Checked Exception - Classes that extend Throwable class except RuntimeException and Error are known as checked exceptions. Checked Exceptions means that compiler forces the programmer to check and deal with the exceptions. e.g.IOException, SQLException etc.
Unchecked Exception - Classes that extends RuntimeException, Error and their subclasses are known as unchecked exceptions e.g. ArithmeticException,NullPointerException, ArrayIndexOutOf Bounds Exception etc.
Error- is irrecoverable should not try to catch. e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc
System errors are thrown by JVM and represented in the Error class. The Error class describes internal system errors. Such errors rarely occur.
Exception describes errors caused by your program and external circumstances. These errors can be caught and handled by your program.
RuntimeException is caused by programming errors, such as bad casting, accessing an out-ofbounds array, and numeric errors.
try { // Code which might throw an exception } catch(Exceptionclass object1) { // code to handle an exception } catch(Exceptionclass object2) { // code to handle an exception } finally { // ALWAYS executed whether an exception was thrown or not }
class Simple { public static void main(String args[]) { int data=50/0; System.out.println("rest of the code..."); } }
Exception in thread main java.lang.ArithmeticException:/ by zero
Rest of the code is not executed (rest of the code..)statement is not printed
-Prints out exception description.
-Prints the stack trace (Hierarchy of methods where the exception occurred).
-Causes the program to terminate.
class Simple1 { public static void main(String args[]) { try { int data=50/0; } catch(ArithmeticException e) { System.out.println(e); } System.out.println("rest of the code..."); }}
Exception in thread main java.lang.ArithmeticException:/ by zero rest of the code…
try { // Code which might throw an exception } catch(Exceptionclass object1) { // code to handle an exception } catch(Exceptionclass object2) { // code to handle an exception }
try { statement 1; statement 2; try { statement 1; statement 2; } catch(Exception e) { } } catch(Exception e) { }
class Excep6 { public static void main(String args[]) { try { try { S.o.p("going to divide"); int b =39/0; } catch(ArithmeticException e) { System.out.println(e); } try { int a[]=new int[5]; a[5]=4; } catch(ArrayIndexOutOfBoundsException e) { System.out.println(e); } System.out.println("other statement"); } catch(Exception e) { System.out.println("handeled"); } System.out.println("normal flow.."); } }
class Simple { public static void main(String args[]) { try { int data=25/0; System.out.println(data); } catch(ArithmeticException e) { System.out.println(e); } finally { System.out.println("finally block is always executed"); } System.out.println("rest of the code..."); }}>
throw new ThrowableInstance
-Using a parameter into a catch clause
-Creating one with the new operator
public class bank { public static void main(String args[]) { int balance = 100, withdraw = 1000; if(balance < withdraw) { //ArithmeticException e = new ArithmeticException("No money please"); //throw e; //throw new ArithmeticException(); throw new ArithmeticException("No Money"); } else { System.out.println("Draw & enjoy Sir, Best wishes of the day"); } } }
import java.io.*; public class Example { public static void main(String args[]) throws IOException { DataInputStream dis=new DataInputStream(System.in); int x = Integer.parseInt(dis.readLine()); if(x < 0) { throw new IllegalArgumentException(); throw new IllegalArgumentException ("You have entered no"+" "+ x +" "+ "which is less than 0"); } else { System.out.println("The no is"+x); } } }
type method-name parameter-list) throws exception-list {
// body of method
}
throw | throws |
throw is used to explicitly throw an exception. | throws is used to declare an exception. |
checked exception can not be propagated without throws. | checked exception can be propagated with throws. |
throw is followed by an instance. | throws is followed by class |
throw is used within the method. | throws is used with the method signature. |
You cannot throw multiple exception | You can declare multiple exception e.g. public void method()throws IOException,SQLException. |
class NumberRangeException extends Exception { String msg; NumberRangeException() { msg = new String("Enter a number between 20 and 100"); } } public class My_Exception { public static void main (String args [ ]) { try { int x = 10; if (x < 20 || x >100) throw new NumberRangeException( ); } catch (NumberRangeException e) { System.out.p