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

java eh

Uploaded by

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

java eh

Uploaded by

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

An exception (or exceptional event) is a problem that arises during the execution

of a program. When an Exception occurs the normal flow of the program is disrupted
and the program/Application terminates abnormally

Exception Handling is a mechanism of specifying what to do when something


unexpected happens

Error:
------
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError
etc.

Types of Java 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.

____________________________________________________
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 and/or finally.

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.

finally:
The "finally" block is used to execute the important code of the program. It
is executed whether an exception is handled or not.

throw:
The "throw" keyword is used to raise an exception programatically. throw
keyword is mainly used to raise user defined exceptions.

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.

try-catch:-
-----------
try{
statement(s);
}catch(<ExceptionType> <referencevariable>){
handling_statement(s);
}catch(<ExceptionType> <referencevariable>){
handling_statement(s);
}
-----------------------
-----------------------

__________________________________________________________

try-catch-finally:
------------------

try{
statement(s);
}catch(<ExceptionType> <referenceVariable>){
handling_statement(s);
}catch(<ExceptionType> <referenceVariable>){
handling_statement(s);
}
-----------------------
-----------------------
finally{
statement(s);
}

________________________________________________________________
try-finally:
------------------

try{
statement(s);
}finally{
statement(s);
}

__________________________________________________________________

example:
--------
class Program{
public static void main(String[] args){
int a=10, b=0,c;
c=a/b;
System.out.println("Division = "+c);
}
}

o/p:
---

D:\javaex>java Program
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Program.main(Program.java:4)
_________________________________________________________________
applying try-catch:
------------------
class Program{
public static void main(String[] args){
int a=10, b=0,c;
try{
c=a/b;
}catch(ArithmeticException ex){
System.out.println("Denominator can't be zero!");
c=0;
}
System.out.println("Division = "+c);
}
}

__________________________________________________________________
try with multiple catch clauses:
--------------------------------

class Div{
public static void main(String[] args){
int a, b, c;
try{
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
c=a/b;
System.out.println("Division = "+c);
}catch(ArrayIndexOutOfBoundsException ex){
System.out.println("Usage: java Div num1 num2");
}catch(NumberFormatException ex){
System.out.println("Input Should be Integers");
}catch(ArithmeticException ex){
System.out.println("Denominator can't be zero!");
}
}
}
___________________________________________________________________
Nested / inner try-catch;
------------------------
class Div{
public static void main(String[] args){
int a, b, c;
try{
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
try{
c=a/b;
System.out.println("Division = "+c);
}catch(ArithmeticException ex){
System.out.println("Denominator can't be zero!");
}
}catch(ArrayIndexOutOfBoundsException ex){
System.out.println("Usage: java Div num1 num2");
}catch(NumberFormatException ex){
System.out.println("Input Should be Integers");
}
}
}
___________________________________________________________________
the super "Exception" class:
----------------------------
-> the Exception catch block should be the only catch block or it should be the
last catch block
class Div{
public static void main(String[] args){
int a, b, c;
try{
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
c=a/b;
System.out.println("Division = "+c);
}catch(ArithmeticException ex){
System.out.println("Denominator can't be zero!");
}catch(Exception ex){
System.out.println("Exception Caught: "+ex.getMessage());
}
}
}
___________________________________________________________________

You might also like