10
© 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 TM

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

Embed Size (px)

Citation preview

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

© 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 [email protected]

TM

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

© 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.

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

© 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.

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

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

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

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

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

© Mohamed Nuzrath

Handling Exceptions

The exception handling mechanism in Java is made up of two parts

- throwing exceptions and- catching them

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

© 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.

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

© 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.

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

© 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.

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

© 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.java