0% found this document useful (0 votes)
25 views

5 Exceptions

This document provides an overview of exception handling in programming. It defines exceptions as logical or runtime errors that disrupt normal program execution. It describes try, catch, and finally blocks for handling exceptions, as well as throw for raising exceptions and throws for specifying exceptions in method declarations. It also distinguishes between checked and unchecked exceptions, and explains how to create user-defined exceptions by extending the Exception class.

Uploaded by

K Ranjith Reddy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

5 Exceptions

This document provides an overview of exception handling in programming. It defines exceptions as logical or runtime errors that disrupt normal program execution. It describes try, catch, and finally blocks for handling exceptions, as well as throw for raising exceptions and throws for specifying exceptions in method declarations. It also distinguishes between checked and unchecked exceptions, and explains how to create user-defined exceptions by extending the Exception class.

Uploaded by

K Ranjith Reddy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

Presentation by:

Chakradhar

Exception Handling

PROGRAMMING ERRORS

Syntax Errors /
Compilation Errors

Logical Errors /
Runtime Errors

Exception

Exception Handling
Exception is Logical Error/ Runtime Error which abrupt the execution of application. Depending on provided values in the statement, an exception may or may not occur. Ex: c = a / b ; If any statement is going throw an Exception, it should be properly handled to overcome the abruption of the application.

try
Try is block of statements which may throw exceptions. The statements throws an exception or exceptions raising must be there in try block to handle it.

One try block can have any number of handlers.

catch
Catch is an handler of exception thrown from the try block. Catch should immediately follow the try block. One try can have one catch or any number of catches but they should be continues.

try {
statements;

} catch(XXXException e) { handling statements; } catch(YYYException e) { handling statements; }

finally is used to execute a code at any case ie., either exception is thrown or not thrown from try block. finally can be there immediately after try block or if handlers are provided then this can be written after catch block. try{.}

finally

try{} finally{..}

catch(Exception e){..}
finally{..}

throw
It is used to throw an Object of Exception type or any User Defined Exceptions. throw must be used from try block only to handle it properly. Exception e=new Exception(); Throw e; or throw new Exception();

Exception
Checked Exception
Examples: IOException RemoteException

Unchecked Exception
Examples: ArithmeticException FileNotFoundException

SocketException
AllUserDefined

NumberFormatException
ArrayIndexOutOfBounds

Exceptions

Exception

throws
throws is used to specify Exception in method declaration from where the checked exceptions are thrown.
syntax:
returntype method(Args) throws CheckedException { statements; }

User Defined Exceptions


A class extended from Exception by overriding the toString() method. These are used to raise an exception on some conditions, restrictions, validations etc.

All the user defined exceptions are Checked Exceptions which must be specified with a throws keyword.

class MyException extends Exception {


properties; constructor();

public String toString() { return Statement; } }

User Defined Exceptions


raising MyException on a condition if(value<=0) { MyException me=new MyException(); throw me; }

You might also like