Docsity

Understanding Java 2-D Arrays, Initializers, and Exceptions, Slides of Object Oriented Programming

Partial preview of the text

Download Understanding Java 2-D Arrays, Initializers, and Exceptions and more Slides Object Oriented Programming in PDF only on Docsity!

Recitation

2 - D arrays.

Exceptions

Animal[] v= new Animal[ 3 ];

declaration of

array v

v null

Create array of 3

elements

a

Animal[]

null

null

null

Assign value of

new-exp to v

a

Assign and refer to elements as usual:

v[0]= new Animal(…);

a= v[0].getAge();

(Review: showed this in Lecture 6)

v.length is the size of the array

Two-dimensional arrays

int[] c= new int [5]; c is a 1-dimensional array

int[] d= new int [5, 8]; You would think this gives an

array/table with 5 rows and 8

columns. BUT Java does it

differently

int[] d= new int [5][8]; Java does it like this

d.length number of rows (5)

d[0].length number of columns in row 0 (8)

d[1].length number of columns in row 1 (8)

d[2][0]= 6; Store 6 in element d[2][0].

Java has only 1-dimensional arrays —whose elements can be arrays

b a

a

null

null

int [][] b; Declare variable b of type int [][]

b= new int [2][] Create a 1-D array of length 2 and store its

name in b. Its elements have type int [] (and start as null ).

In Java, there are really only

1 - dimensional arrays, whose

elements can be arrays!

int [][]

int [][]

Division by 0 causes an “Exception to be thrown”.

program stops with output:

public static void main(String[] args) {

int b= 3/0;

Exception in thread "main"

java.lang.ArithmeticException: / by zero

at C.main(C.java:7)

Exceptions

The “Exception”

that is “thrown” Happened in main, line 7

This is line 7

parseInt throws a NumberFormatException

public static void main(String[] args) {

… code to store a string in s —expected to be an int

int b= Integer.parseInt(s);

/** Parse s as a signed decimal integer and return

the integer. If s does not contain a signed decimal

integer, throw a NumberFormatException. */

public static int parseInt(String s)

parseInt, when it find an error, does not know what caused the

error and hence cannot do anything intelligent about it. So it

“throws the exception” to the calling method. The normal

execution sequence stops! See next slide

Exceptions and Errors

In Java, there is a class Throwable:

Throwable@x

detailMessage “/ by zero”

getMessage()

Throwable()

Throwable(String)

When some kind of error

occurs, an Exception is

“thrown” —you’ll see

what this means later.

An Exception is an

instance of class Throwable

(or one of its subclasses)

Two constructors in class Throwable. Second one

stores its String parameter in field detailMessage.

Exceptions and Errors

So many different kind of exceptions that

we have to organize them.

“/ by zero”

detailMessage

getMessage()

Exception

RuntimeException

ArithmeticException

Throwable

Exception Error

RuntimeException

ArithmeticException

Do nothing

with these

You can

"handle"

these

Throwable() Throwable(String)

Exc…() Exc…(..)

RunTimeE…() RunTimeE…(…)

Arith…E…() Arith…E…(…)

Throwable@x

Subclass: 2 constructors, no

other methods, no fields.

Constructor calls superclass

constructor

public class Ex {

public static void main(…) {

second();

}

public static void second() {

third();

}

public static void third() {

throw new ArithmeticException("I threw it");

}

}

ArithmeticException: I threw it

at Ex.third(Ex.java:14)

at Ex.second(Ex.java:9)

at Ex.main(Ex.java:5)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(…)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(…)

at java.lang.reflect.Method.invoke(Method.java:585)

AE

a

AE

a

AE

a

Class:

Call

Output

Ex.main();

throw statement

docsity.com

/** An instance is an exception */

public class OurException extends Exception {

/** Constructor: an instance with message m*/

public OurException(String m) {

super (m);

/** Constructor: an instance with no message */

public OurException() {

super();

How to write an exception class

The “ throws ” clause

/** Class to illustrate exception handling */

public class Ex {

public static void main() throws OurException {

second();

}

public static void second() throws OurException {

third();

}

public static void third() throws OurException {

throw new OurException("mine");

}

If Java asks for it, insert the throws clause.

Otherwise, don’t be concerned with it.

Try statement: catching a thrown exception

public class Ex1 {

public static void main() throws MyException{

try {

second();

}

catch (MyException ae) {

System.out.println

("Caught MyException: " + ae);

}

System.out.println

("procedure first is done");

}

public static void second() throws MyException {

third();

}

public static void third() throws MyException {

throw new MyException(”yours");

}

}

Execute the try-

block. If it

finishes without

throwing

anything, fine.

If it throws a

MyException

object, catch it

(execute the catch

block); else throw

it out further.