Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Mohamed Nuzrath Java Programming :: Chapter 6 :: Prepared & Presented By :: Mohamed Nuzrath [ Major In Programming ] NCC Programme coordinator IT Lecturer.

Similar presentations


Presentation on theme: "© Mohamed Nuzrath Java Programming :: Chapter 6 :: Prepared & Presented By :: Mohamed Nuzrath [ Major In Programming ] NCC Programme coordinator IT Lecturer."— Presentation transcript:

1 © Mohamed Nuzrath Java Programming :: Chapter 6 :: Prepared & Presented By :: Mohamed Nuzrath [ Major In Programming ] NCC Programme coordinator IT Lecturer | Web Developer | Software Engineer IDM Affiliated University College Kandy Campus #725, Peradeniya Rd, Kandy TP +94 812224621 nuzree@gmail.com TM

2 © Mohamed Nuzrath Exception Handling The general purpose error processing system in Java is known as exception handling mechanism. An exception, which is short for an exceptional condition, is an abnormal condition that can occur during the execution of a program, If these situations aren’t handled properly, the program will abort or it will continue with incorrect result.

3 © Mohamed Nuzrath  The java.lang.Throwable class and its sub classes represent all the exceptions that might occur in a program. When an exceptional condition occurs, a exception is thrown, In other words an instance of class java.lang.Throwable or one of its sub classes is created and passed in to the program.

4 There are two immediate sub classes of java.lang.Throwable, java.lang.Exception and java.lang.Error java.lang.Throwable java.lang.Exceptionjava.lang.Error java.lang.RuntimeException

5  The java.lang.Exception class represents abnormal conditions tht can be handled in programs. The java.lang.RuntimeException class (which is a sub class of java.lang.Exception) and its sub classes represent exceptional conditions that are created during runtime. The java.lang.Error class and its sub classes correspond to situations that are not easily predicted (conditions that should not be expected to occur in normal circumstances) such as system running out of memory. You cant write code and fix errors

6 © Mohamed Nuzrath Handling Exceptions The exception handling mechanism in Java is made up of two parts - throwing exceptions and - catching them

7 © Mohamed Nuzrath Throwing Exceptions  To throw and exception means to signal an error. Exceptions can be thrown by any Java code or by the runtime system. When an exceptional condition occurs, an exception ( an instance of java.lang.Throwable class or one of its sub class) is created and thrown.

8 © Mohamed Nuzrath When an exception is thrown, the runtime system stops the execution Of the program at that point. Then it tries to find a place, to transfer the execution, where the exception can be properly handled.

9 © Mohamed Nuzrath Catching Exceptions After an exception in thrown, the run time system searches for and appropriate exception handler to catch ( to handle ) it. The execution will be transferred to the first handler that the runtime system can find. If the runtime system can’t find any handlers, it will abort the execution of the program.

10 © Mohamed Nuzrath try and catch keywords The “try” keyword is used to specify a block of code, which is capable of throwing exceptions. Immediately after a try block there should be a catch clause or finally clause. Refer CH6\examples\examples\TryCatchDemo.javaexamples\TryCatchDemo.java


Download ppt "© Mohamed Nuzrath Java Programming :: Chapter 6 :: Prepared & Presented By :: Mohamed Nuzrath [ Major In Programming ] NCC Programme coordinator IT Lecturer."

Similar presentations


Ads by Google