0% found this document useful (0 votes)
12 views5 pages

Ch09 1

The document discusses programming error types and exception handling in Java, categorizing errors into compile-time (syntax and static semantic errors) and run-time errors (dynamic semantic and logical errors). It explains the concept of exceptions, their handling, and the distinction between checked and unchecked exceptions, along with keywords used in exception handling such as try, catch, finally, throw, and throws. Additionally, it provides examples to illustrate the usage of exception handling in Java.

Uploaded by

btbtr24122shalvi
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)
12 views5 pages

Ch09 1

The document discusses programming error types and exception handling in Java, categorizing errors into compile-time (syntax and static semantic errors) and run-time errors (dynamic semantic and logical errors). It explains the concept of exceptions, their handling, and the distinction between checked and unchecked exceptions, along with keywords used in exception handling such as try, catch, finally, throw, and throws. Additionally, it provides examples to illustrate the usage of exception handling in Java.

Uploaded by

btbtr24122shalvi
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/ 5

Ch.

9 : Programming Error Types and Exception Handling


9.1 Error: An error is anything in the code that prevents a program from compiling and running
correctly.

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.

Int a=10; // Cannot find symbol class Int. it should be int

int b = a+a++ // Statement must be terminated with semi-colon(;)

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

int a = “Computer Science” // Type incompatibility

B. Run-Time Errors: Errors that occur during execution of the program are run-time errors.

B.1 Dynamic Semantic Errors: Errors which compiler is unable to find.


e.g. Division by Zero , File does not exists , Linkage error etc.
B.2 Logical Errors: A logical error is a mistake that compiles but does not provide the
desired result.

e.g.

(i) for(int i=5 ; i >4 ; i++) // infinite loop

System.out.println( i);

(ii) int sum ( int a , int b)

return a * b; // Returning product of a and b instead of sum

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.

Advantage of Exception Handling


The core advantage of exception handling is to maintain the normal flow of the application. An
exception normally disrupts the normal flow of the application that is why we use exception
handling.

Types of Java Exceptions


There are mainly two types of exceptions: checked and unchecked. Here, an error is
considered as the unchecked exception. According to Oracle, there are three types of
exceptions:

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.

3) Error: Error is irrecoverable . e.g. OutOfMemoryError, VirtualMachineError, AssertionError


etc.

Java Exception Keywords


There are 5 keywords which are used in handling exceptions in Java.

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.

4. throw: The "throw" keyword is used to throw an exception.

5. throws: The "throws" keyword is used to declare exceptions. It doesn't throw an


exception. It specifies that there may occur an exception in the method. It is always used with
method signature.

Java Exception Handling Example

public class JavaExceptionExample


{
public static void main (String args[])
{
try
{
//code that may raise exception
int data=100/0;
}
catch(ArithmeticException e)
{
System.out.println(e);
}
//rest code of the program
finally
{
System.out.println("rest of the code...");
}
}
}
In the above example, 100/0 raises an ArithmeticException which is handled by a try-catch
block.

Difference between throw and throws keywords

throw keyword throws keyword


throw keyword is used throw an exception throws keyword is used in the method
explicitly in the code. signature to declare an exception which
might be thrown by the function while the
execution of the code.
Using throw keyword, we can only propagate Using throws keyword, we can declare both
unchecked exception i.e. the checked checked and unchecked exceptions.
exception cannot be propagated using throw However, the throws keyword can be used to
only. propagate checked exceptions only.
We are allowed to throw only one exception We can declare multiple exceptions using
at a time i.e. we cannot throw multiple throws keyword that can be thrown by the
exceptions. method.

Example:

public class throwVsthrows


{
// defining a user-defined method which throws ArithmeticException
static void method() throws ArithmeticException
{
System.out.println("Inside the method()");
throw new ArithmeticException("throwing ArithmeticException");
}
//main method
public static void main(String args[])
{
try
{
method();
}
catch(ArithmeticException e)
{
System.out.println("caught in main() method");
}
}
}

You might also like