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

Java Programming: Exception Handling

This document discusses exception handling in Java programming. It covers catch blocks, the throw statement, exception and error classes, and the throws statement. Catch blocks contain code to handle specific exceptions, while the throw statement throws exceptions within methods. The try-catch-finally construct is demonstrated for exception handling.

Uploaded by

goyal_anl
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views

Java Programming: Exception Handling

This document discusses exception handling in Java programming. It covers catch blocks, the throw statement, exception and error classes, and the throws statement. Catch blocks contain code to handle specific exceptions, while the throw statement throws exceptions within methods. The try-catch-finally construct is demonstrated for exception handling.

Uploaded by

goyal_anl
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Java Programming

Exception Handling

Incheon Paik

1 Computer Industry Lab.


Java
Contents
? Exception Handling
? catch Block
? throw statement
? Exception and Error Class
? throws statement
? Own Exception

2 Computer Industry Lab.


Java
Exception Handling
class DivideByZero {

public static void main(String args[]) {


a(); Result :
} Exception in thread "main" java.lang.ArithmeticException: / by zero
at DivideByZero.d(DivideByZero.java:22)
static void a() {
b(); at DivideByZero.c(DivideByZero.java:16)
} at DivideByZero.b(DivideByZero.java:12)
at DivideByZero.a(DivideByZero.java:8)
static void b() {
c(); at DivideByZero.main(DivideByZero.java:4)
}

static void c() {


d();
}

static void d() {


int i = 1;
int j = 0;
System.out.println(i / j);
}
}

3 Computer Industry Lab.


Java
Exception Handling
Exception Handling

try {
The codes that have some
// try block possibility to happen exception
}
catch (ExceptionType1 param1) {
// Exception Block
}
catch (ExceptionType2 param2) { Carry out these codes when the
// Exception Block corresponding exception occurred.
}
……
catch (ExceptionTypeN paramN) {
// Exception Block
}
finally { Do always
// finally Block
}

4 Computer Industry Lab.


Java
Exception Handling
class Divider { finally {
System.out.println("Finally block");
public static void main(String args[]) { }
}
try { }

System.out.println("Before Division");
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
Result : java Divider
System.out.println(i / j);
System.out.println("After Division"); Before Division
} ArrayIndexOutOfBoundsException
catch (ArithmeticException e) { Finally block
System.out.println("ArithmeticException");
}
catch (ArrayIndexOutOfBoundsException e) { Result : java Divider 1 0
System.out.println("ArrayIndex" + Before Division
"OutOfBoundsException");
ArithmeticException
}
catch (NumberFormatException e) { Finally block
System.out.println("NumberFormatException");
}

5 Computer Industry Lab.


Java
Searching catch Block
class CatchSearch { public static void b() { public static void d() {
try { try {
public static void main(String args[]) System.out.println("Before c"); int array[] = new int[4];
{
c();
try { array[10] = 10;
System.out.println("After c");
System.out.println("Before a"); }
}
a();
catch catch (ClassCastException e) {
System.out.println("After a"); (ArrayIndexOutOfBoundsException e)
{ System.out.println("d: " + e);
}
System.out.println("b: " + e); }
catch (Exception e) {
}
System.out.println("main: " + e); finally {
finally {
} System.out.println("d: finally");
System.out.println("b: finally");
finally {
} }
System.out.println("main: finally");
} }
}
} }
public static void c() {
try { Result :
public static void a() {
System.out.println("Before d"); Before a
try {
d(); Before b
System.out.println("Before b");
System.out.println("After d"); Before c
b();
} Before d
System.out.println("After b");
catch (NumberFormatException e) { d: finally
}
System.out.println("c: " + e);
catch (ArithmeticException e) { c: finally
}
System.out.println("a: " + e); b: java.lang.ArrayIndexOutOfBoundsException: 10
finally {
} b: finally
System.out.println("c: finally");
finally { After b
}
System.out.println("a: finally"); a: finally
}
}
After a
}
main: finally
6 Computer Industry Lab.
Java
throw Statement
throw Statement
throw object;

Exception Object in argument


catch (ExceptionType param) {
….
throw param;
… ..
}

Throw to new Exception Object


throw new ExceptionType(args);

7 Computer Industry Lab.


Java
Throw Statement
public static void d() {
class ThrowDemo { public static void b() {
try {
try { int i = 1;
public static void main(String args[]) System.out.println("Before c"); int j = 0;
{
c(); System.out.println("Before division");
try {
System.out.println("After c"); System.out.println(i / j);
System.out.println("Before a"); System.out.println("After division");
}
a(); }
catch (ArithmeticException e) {
System.out.println("After a"); catch (ArithmeticException e) {
System.out.println("b: " + e);
} System.out.println("d: " + e);
}
catch (ArithmeticException e) { throw e;
finally { }
System.out.println("main: " + e);
System.out.println("b: finally"); finally {
}
} System.out.println("d: finally");
finally {
} }
System.out.println("main: finally");
}
} }
public static void c() {
}
try { Result :
System.out.println("Before d"); Before a
public static void a() { Before b
d();
try { Before c
System.out.println("After d");
System.out.println("Before b"); Before d
}
b(); Before division
catch (ArithmeticException e) {
System.out.println("After b"); d: java.lang.ArithmeticException: / by zero
System.out.println("c: " + e); d: finally
}
throw e; c: java.lang.ArithmeticException: / by zero
catch (ArithmeticException e) {
} c: finally
System.out.println("a: " + e);
finally { b: java.lang.ArithmeticException: / by zero
}
System.out.println("c: finally"); b: finally
finally { After b
}
System.out.println("a: finally"); a: finally
}
} After a
main: finally
8 Computer Industry Lab.
Java
Throw Statement
class ThrowDemo2 { public static void a() {
try {
public static void main(String args[]) { System.out.println("Before throw statement");
try { throw new ArithmeticException();
System.out.println("Before a"); }
a(); catch (Exception e) {
System.out.println("After a"); System.out.println("a: " + e);
} }
catch (Exception e) { finally {
System.out.println("main: " + e); System.out.println("a: finally");
} }
finally { }
System.out.println("main: finally"); }
}
}
Result :
Before a
Before throw statement
a: java.lang.ArithmeticException
a: finally
After a
main: finally

9 Computer Industry Lab.


Java
Exception and Error Class
Throwable Constructor Subclasses of Exception Class
ClassNotFoundException
Throwable()
IllegalAccessException
Throwable(String message)
InstantiationException
InterruptedException
getMessage() Method NoSuchFieldException
NoSuchMethodException
String getMessage()
RuntimeException

Subclasses of RuntimeException
printStackTrace() Method ArrayIndexOutOfBoundsException

void printStackTrace() ArithmeticException


ClassCastException
NegativeArraySizeException
Exception Constructor NullPointerException
NumberFromatException
Exception()
Exception (String message) SecurityException
StringIndexOutOfBoundsException

10 Computer Industry Lab.


Java
Exception and Error Class
public static void c() {
class PrintStackTraceDemo { try {
d();
public static void main(String args[]) { }
try { catch (NullPointerException e) {
a(); e.printStackTrace();
} }
catch (ArithmeticException e) { }
e.printStackTrace();
} public static void d() {
} try {
int i = 1;
public static void a() { int j = 0;
try { System.out.println(i / j);
b(); }
} catch (NullPointerException e) {
catch (NullPointerException e) { e.printStackTrace();
e.printStackTrace(); }
} }
} }
Result:
public static void b() { java.lang.ArithmeticException: / by zero
try {
c(); at PrintStackTraceDemo.d(PrintStackTraceDemo.java:43)
} at PrintStackTraceDemo.c(PrintStackTraceDemo.java:32)
catch (NullPointerException e) {
e.printStackTrace(); at PrintStackTraceDemo.b(PrintStackTraceDemo.java:23)
} at PrintStackTraceDemo.a(PrintStackTraceDemo.java:14)
}
at PrintStackTraceDemo.main(PrintStackTraceDemo.java:5)
11 Computer Industry Lab.
Java
throws Statement
public static void b() throws ClassNotFoundException {
throws Statement of Constructor c();
consModifiers clsName(cparams) thorws exceptions { }
// constructor body
public static void c() throws ClassNotFoundException {
}
Class cls = Class.forName("java.lang.Integer");
System.out.println(cls.getName());
throws Statement at Method System.out.println(cls.isInterface());
}
mthModifiers rtype mthName(mparams) thorws exceptions { }
// constructor body
}

class ThrowsDemo {
public static void main(String args[]) {
a();
}
public static void a() {
try {
b();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

12 Computer Industry Lab.


Java
throws Statement
public static void b() throws ClassNotFoundException {
throws Statement of Constructor c();
consModifiers clsName(cparams) thorws exceptions { }
// constructor body
public static void c() throws ClassNotFoundException {
}
Class cls = Class.forName("java.lang.Integer");
System.out.println(cls.getName());
throws Statement at Method System.out.println(cls.isInterface());
}
mthModifiers rtype mthName(mparams) thorws exceptions { }
// constructor body
} Result :
Java.lang.Integer
class ThrowsDemo { false
public static void main(String args[]) {
a();
}
public static void a() {
try {
b();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

12 Computer Industry Lab.


Java
Own Exception
static void c() throws ExceptionA, ExceptionB {
Subclass of Exception Random random = new Random();
int i = random.nextInt();
if (i % 2 == 0) {
import java.util.*; throw new ExceptionA("We have a problem");
}
class ExceptionSubclass { else {
throw new ExceptionB("We have a big problem");
public static void main(String args[]) { }
a(); }
} }
static void a() { class ExceptionA extends Exception {
try { public ExceptionA(String message) {
b(); super(message);
} }
catch (Exception e) { }
e.printStackTrace();
}
} class ExceptionB extends Exception {
public ExceptionB(String message) {
static void b() throws ExceptionA { super(message);
try { }
c(); }
}
catch (ExceptionB e) { Execution:
e.printStackTrace(); ExceptionA: We have a problem
}
at ExceptionSubclass.c(ExceptionSubclass.java:31)
}
at ExceptionSubclass.b(ExceptionSubclass.java:20)
at ExceptionSubclass.a(ExceptionSubclass.java:11)
at ExceptionSubclass.main(ExceptionSubclass.java:6)

13 Computer Industry Lab.


Java
Exercise
? Step 1 (Making Some Method for Exception)
Slide 7,8,9, 12
static int thrower(String s) ...... {
try {
if (s.equals("divide")) {
// Write the code for raising Divide by Zero Exception
}
if (s.equals("null")) {
// Write the code for raising NullPoint Exception
}
if (s.equals("test"))
// Write the code for raising TestException
return 0;
} finally {
// Some code for print out the current message
}

14 Computer Industry Lab.


Java
Exercise
Step 2, Writing the Exception Handler 1
while(true) {
try {
istream = new FileInputStream(inputfilename);
break;
} catch(java.io.FileNotFoundException e) {
System.out.print("File not found. Re-Enter file name : ");
inputfilename = charStream.readLine().trim();
}

} // end of while

Step 3, Writing the Exception Handler 2


Slide # 7,8,9, 12

15 Computer Industry Lab.


Java

You might also like