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

Lab 6

This lab guide covers exception handling in Java, including unchecked and checked exceptions. It contains exercises on catching exceptions, defining exception classes, and using nested classes. The exercises guide students to distinguish exception types, catch exceptions, and define exception classes and nested classes.

Uploaded by

Hồng Quân
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lab 6

This lab guide covers exception handling in Java, including unchecked and checked exceptions. It contains exercises on catching exceptions, defining exception classes, and using nested classes. The exercises guide students to distinguish exception types, catch exceptions, and define exception classes and nested classes.

Uploaded by

Hồng Quân
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

LAB GUIDE – SEMESTER 2 – COURSE: Java

Programming I
LAB: 06
JPI - Lab 6 – Exception and more class

Java Programming I
Lab 6

Objectives:
In this session, you will be practicing with:

❖ Catch error handling programming using Exception

❖ Define and use Exception

❖ Nested classes in Java

Part I: Workshop – 15 minutes - if not done

Part II: Step by step – 45 minutes


Exercise 1: Catch error handling programming using Exception: (10 minutes)
/**
* Write a description of class Client here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Client {
/**
* Constructor for objects of class Client
*/
public Client() {
// To do:
}

public void doUnchecked(String value) {


// Can phai check exception, neu khong --> bug
int result=canThrowUncheckedException(value);
System.out.println("result="+result);
}

private int canThrowUncheckedException(String value) {


return Integer.parseInt(value);
}

public void doChecked() {


try {
// Buoc phai check exceptions o day! Khong cach nao khac
canThrowCheckedException();
System.out.println("OK");
}catch(Exception ex) {
System.out.println(ex);
}
}

2
JPI - Lab 6 – Exception and more class
private int canThrowCheckedException() throws Exception{
throw new Exception("Failure");
}
}

Questions:
● Distinguishing un checked Checked Exception and Exception?

● Using Checked Exceptions and using unchecked Exceptions?

● Why should not catch (Exception ex)?

Exercise 2: Unchecked Exception: (20 minutes)


/**
* Write a description of class Client here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class UncheckedException{
/**
* Constructor for objects of class Client
*/
public UncheckedException () {
// To do:
}

public static void main(String[] args) {


int i, n = 2;
int a[] = new int[n];

//Declare Scanner Object named input


java.util.Scanner input = new java.util.Scanner(System.in);

for(i=0; i<=n; i++)


{
System.out.printf("a[%d] = ", i);
a[i] = input.nextInt();
}
}
}

Questions:
✔ Compile and run the test class.

✔ How many Exceptions may occur in the above code?

✔ Please correct the above code to be able to catch every Exception that.

Exercise 3: (10 minutes)


/**
* Write a description of class Client here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MoreClass{

3
JPI - Lab 6 – Exception and more class
public static void main(String[] args) {
Object obj1 = new Object();
System.out.println(obj1);

Object obj2 = new Object(){


public String toString(){
return "this is obj2";
}
};
System.out.println(obj2);
}
}

Questions:
✔ Compile and run the test class.

✔ Why System.out.println(obj1); be displayed?

✔ What's the difference between two objects obj1 and obj2?

Part III: Do it yourself – 60 minutes


Exercise 1:

I
Exercise 2:

Part IV: Homework


Exercise 12,13,14
References
1) Onlive varsity Java Programming I, Aptech Education
2) http://www.java2s.com

You might also like