Java Beginners - Java For Beginners Java Exception Rating: 5.0/5 (1 vote cast) | Level : Beginners Author : Arunkumar S Download Source : exception.zip
Java Exception - What Exceptions AreExceptions are objects that describe any error caused by an external resource not being available or an internal processing problem. They are passed to exception handlers written by the programmer to enable graceful recovery. If the handler has not been written, the program will terminate with a display of the Exception class. There are many exception classes such as IOException and NumberFormatException.
Exception Handling Exception handling is a method of trapping or coping with anticipated errors (system, data entry or calculation) and handling or dealing with them in a graceful manner. The Exception class of objects offers a rich group of subclasses to trap specific errors and recover from them. A thread is the flow of execution of a single set of program statements. Multithreading consists of multiple sets of statements which can be run in parallel. With a single processor only one thread can run at a time but strategies are used to make it appear as if the threads are running simultaneously. Depending on the operating system, either timeslicing or interrupt methods will move the processing from one thread to the next.
Checked and Unchecked Exceptions
- Two kinds of exceptions, checked and unchecked.
- Anything from a runtime exception is an unchecked exception. Meaning you don't have to provided a try/catch block. Subclasses of Error or RuntimeException.
try/catch block The code that might throw an exception is enclosed in the try block. One or more catch clauses can be provided to handle different exception types: try { // code that might throw exceptions } catch(Exception e) { //code to handle the exception }
finally block The finally block can also be provided to perform any cleanup operation. If an exception is thrown, any matching catch clauses are executed, then control comes to the finally block, if one is provided. The syntax of the finally block is as follows: try { // code that throws exceptions } catch(Exception e) { // handle exception } finally { // cleanup code } The finally block is executed even if no exception is thrown. The only case in which this does not happen is when System.exit() is invoked by the try or catch blocks. A try block should have either a catch block or a finally block, but it's not required to have both. Example /** * * @author Arunkumar Subramaniam * */ public class Test { public static void main(String s[]){ try{ int i=0; i=10/i; }catch(Exception e){ System.out.println(e); } } }
or /** * * @author Arunkumar Subramaniam * */ public class Test { public static void main(String s[]){ try{ int i=0; i=10/i; }catch(ArithmeticException e){ System.out.println(e); }catch(Exception e){ System.out.println(e); } } }
Throws And Throw
throw is used to throw an exception manually, where as throws is used in the case of checked exceptions, to reintimate the compiler that we have handled the exception. so throws is to be used at the time of defining a method and also at the time of calling that function, which rises an checked exception. /** * * @author Arunkumar Subramaniam * */ public class Test { void hello() throws ArithmeticException ,Exception { try{ int i=0; i=10/i; }catch(ArithmeticException e){ throw new ArithmeticException(); } } void altHello() throws ArithmeticException ,Exception{ int i=0; i=10/i; } public static void main(String s[]){ Test t=new Test(); try { t.altHello(); } catch (ArithmeticException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } try { t.hello(); } catch (ArithmeticException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } 1 | 2 | Next >
Discussion about this tutorial
 Start a new Discussion | Read All Discussion
|