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

Ch10exaption Handling in Java

This document discusses exception handling in Java. It describes different types of exceptions including compile-time and run-time errors. It explains the try, catch, and finally keywords used in exception handling and their purpose.

Uploaded by

ks5158660
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Ch10exaption Handling in Java

This document discusses exception handling in Java. It describes different types of exceptions including compile-time and run-time errors. It explains the try, catch, and finally keywords used in exception handling and their purpose.

Uploaded by

ks5158660
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Chapter 10

~ -- - - - "= - - '='

Exception Handling in Java

Quick Chapter Summary


t We believe that compiled program is error free and will
always execute successfully.
t But in few cases the program can ~ te while it ls
executing.
Example :
t Program that connects to a particular website and
download web pages, but suppose there is no internet I I I
t Dealing with files and tries to modify a read-only file I 11
t User tries to open a file that does not exist in system 111
CT]
Types of Exceptions
~--
• In Java, all kinds of error conditions are called Exceptions.

t Errors can be broadly classified into 2 categories :


(1) Compile time errors

(2) Run time errors

t The exception results in an abnormal exception and it may


lead to abnormal termination of the program.

t It usually signals an error.


Compile -time errors
--=-- -
t Compiler is used to convert source code into object code.
Example of Errors :

• Syntax error

• Missing comma "," or semicolon ";"

• Use of undeclared variable

• Wrong spellings of identifier or keyword

• Mismatch of bracket
Run - time errors
t If there are no syntax errors in the source code then the
program will compile successf ully and will get a9 file.
t However this does no• guarantee that the program will
execute as per our expectations.
Example :
t In the program, we have created an array citylist[).
tThat contains name of four different cities.
t We are trying to display the contents of element of citylist
array that does not exist.
[TI
e Chapter 1 Creating HTM X

Program Executed Successfully


t In
-
Computer Science, Exit code or Exit status indicates

whether the command or a program executed successfully

or not.

t Exit code "O"


Command executed successfully.

t Exit code "1"


Some problem occurred while executing the command.

[TI
Exception Handling
t An exception is an error condition.
t Java uses 3 keywords to writeari'exception handler :
(1) try
-
: Contains the code to one or more exceptions.

(2) catch : Contains the code to handle exceptions of

particula~ e.

(3) finally: Always executed before the program ends.

IT]
e How to download kompo X

try block
IHry statement contains a block of statements within the

~r~ o)
t A try block may give rise to one or more exceptions.
t Syntax of try block is : try { \ ~
t After try statement, there has to be either a catch block or
a finally block.

IT]
catch block
tcatch block must immediately follow the try block.

t It contains the code that is to be executed to handle an


exception.

tcatch block is an exception handler.

CT]
e Windows 11 New Featur X

fi~ I
t finally block is generally used to ~ t the end of
executing a try block.

tfinally block is widely used if a file need to be closed or a


critical resource is to be released at the completion of the
program.
throw statement
t throw keyword is used to explicitly throw an Exception
object.

t Java Virtual Machine (JVM) created an exception object


and was t hrowing it automatically.

Example :

t An object of ArithmeticException was created when we


tried to perform a divide by zero operation.

You might also like