Ch09 1
Ch09 1
Type of Programming Errors: Broadly there are two types of errors Compile –time errors
and Run-time errors.
Syntax Errors
Compile Time Errors
Static Semantic Errors
Errors
Logical Errors
Run Time Errors
Dynamic Semantic Errors
A. Compile-Time Errors: Errors that occur at the time of compilation (before execution) are
called Compile-time errors.
A.1 Syntax Errors: Syntax errors occur when grammatical rules of a programming language
are violated (misused).
e.g.
A.2 Static Semantic Errors: Semantic errors occur when statements are not meaningful.
e.g.
a+b=c; // On the left side of an Assignment operator(=) , only single variable is allowed
B. Run-Time Errors: Errors that occur during execution of the program are run-time errors.
e.g.
System.out.println( i);
9.2 Exception: In Java, an exception is an event that disrupts the normal flow of the
program. It is an object which is thrown at runtime.
Exception Handling : The Exception Handling in Java is one of the powerful mechanism to
handle the runtime errors such as ClassNotFoundException, IOException, SQLException,
RemoteException, etc . so that normal flow of the application can be maintained.
1) Checked Exception: The classes which directly inherit Throwable class except
RuntimeException and Error are known as checked exceptions.
e.g. IOException, SQLException etc. Checked exceptions are checked at compile-time.
2) Unchecked Exception: The classes which inherit RuntimeException are known as
unchecked exceptions.
e.g. ArithmeticException, NullPointerException , ArrayIndexOutOfBoundsException etc.
Unchecked exceptions are not checked at compile-time, but they are checked at runtime.
1. try: The "try" keyword is used to specify a block where we should place exception code.
The try block must be followed by either catch or finally. It means, we can't use try block
alone.
2. catch: The "catch" block is used to handle the exception. It must be preceded by try block
which means we can't use catch block alone. It can be followed by finally block later.
3. finally: The "finally" block is used to execute the important code of the program. It is
executed whether an exception is handled or not.
Example: