ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3114Packages Exception Han…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3114

    Advanced Computer Programming

    Progress0 / 12 done
    Topics12
    1Abstract classes and Interfaces2Packages and Exception handling3Advanced issues of GUI and event handling4Applets and Swing
    5JDBC6Multithreading7Building Client/Server and implementing protocols8RMI (Remote Method Invocation)9Java Secure Socket Extension and Secure Sockets Layer (SSL)10SSL Socket and SSL Server Socket classes
    11HTTPS12Developing TCP/IP client and server with telnet
    4 min read
    Topic 2 of 12
    Topic Study Note Illustration
    02

    Packages and Exception handling

    A package in Java is a namespace (folder) that organizes related classes and interfaces into a hierarchical structure .

    πŸ“˜ Java API: Packages and Exception Handling


    πŸ”· PART 1: PACKAGES IN JAVA


    πŸ”Ή 1. Definition

    A package in Java is a namespace (folder) that organizes related classes and interfaces into a hierarchical structure.

    πŸ‘‰ It helps in:

    • Avoiding name conflicts
    • Making code reusable
    • Improving organization

    πŸ”Ή 2. Types of Packages

    βœ… Built-in Packages

    Provided by Java

    Examples:

    • java.lang β†’ basic classes (String, Math)
    • java.util β†’ utilities (Scanner, ArrayList)
    • java.io β†’ input/output

    βœ… User-defined Packages

    Created by programmer


    πŸ”Ή 3. Creating a Package

    βœ” Syntax:

    package mypackage;
    

    βœ” Example:

    package mypackage;
    
    public class Hello {
        public void show() {
            System.out.println("Hello from package");
        }
    }
    

    πŸ”Ή 4. Compiling and Running

    Step-by-step:

    1. Save file β†’ Hello.java
    2. Compile:
    javac -d . Hello.java
    
    1. Run:
    java mypackage.Hello
    

    πŸ”Ή 5. Accessing Packages

    βœ” Using import

    import mypackage.Hello;
    

    OR

    import mypackage.*;
    

    βœ” Example:

    import mypackage.Hello;
    
    class Test {
        public static void main(String[] args) {
            Hello h = new Hello();
            h.show();
        }
    }
    

    πŸ”Ή 6. Access Modifiers in Packages

    Modifier Same Class Same Package Different Package
    public βœ” βœ” βœ”
    protected βœ” βœ” βœ” (via inheritance)
    default βœ” βœ” ✘
    private βœ” ✘ ✘

    πŸ”Ή 7. Advantages of Packages

    • Code reusability
    • Better organization
    • Access control
    • Avoids naming conflicts

    πŸ”Ή 8. Diagram Description

    πŸ“Š Package Structure Diagram:

    • Main folder β†’ package name
    • Inside β†’ classes (files)
    • Tree structure showing hierarchy

    πŸ”· PART 2: EXCEPTION HANDLING IN JAVA


    πŸ”Ή 1. Definition

    An exception is an error or abnormal condition that occurs during program execution and disrupts normal flow.

    πŸ‘‰ Example:

    • Division by zero
    • File not found

    πŸ”Ή 2. Types of Exceptions

    βœ… Checked Exceptions

    • Checked at compile-time
    • Must be handled

    Examples:

    • IOException
    • SQLException

    βœ… Unchecked Exceptions

    • Occur at runtime

    Examples:

    • ArithmeticException
    • NullPointerException

    πŸ”Ή 3. Exception Handling Keywords

    Keyword Purpose
    try Code that may cause exception
    catch Handles exception
    finally Always executes
    throw Explicitly throw exception
    throws Declare exception

    πŸ”Ή 4. Basic Syntax

    try {
        // risky code
    } catch (Exception e) {
        // handling code
    } finally {
        // always runs
    }
    

    πŸ”Ή 5. Example

    public class Test {
        public static void main(String[] args) {
            try {
                int a = 10 / 0;
            } catch (ArithmeticException e) {
                System.out.println("Cannot divide by zero");
            } finally {
                System.out.println("Done");
            }
        }
    }
    

    βœ” Explanation

    1. 10 / 0 β†’ causes exception
    2. Control goes to catch
    3. Error handled
    4. finally always executes

    πŸ”Ή 6. Using throw

    throw new ArithmeticException("Error occurred");
    

    πŸ”Ή 7. Using throws

    void readFile() throws IOException {
        // code
    }
    

    πŸ”Ή 8. Multiple Catch Blocks

    try {
        int a[] = new int[5];
        a[10] = 30;
    } catch (ArithmeticException e) {
        System.out.println("Arithmetic Error");
    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println("Index Error");
    }
    

    πŸ”Ή 9. Custom Exception

    class MyException extends Exception {
        public MyException(String msg) {
            super(msg);
        }
    }
    

    πŸ”Ή 10. Important Rules

    • Only one finally block
    • Multiple catch allowed
    • finally always executes (except system crash)
    • Checked exceptions must be handled

    πŸ”Ή 11. Diagram Description

    πŸ“Š Exception Flow Diagram:

    1. Try block executes
    2. Exception occurs
    3. Control jumps to catch
    4. Finally block executes

    πŸ“ Likely Exam Questions

    1. Define package in Java.
    2. What are advantages of packages?
    3. Explain how to create a package.
    4. What is exception handling?
    5. Types of exceptions in Java.
    6. Explain try-catch-finally with example.
    7. Difference between throw and throws.
    8. What are checked and unchecked exceptions?
    9. Write a program demonstrating exception handling.
    10. Explain access modifiers in packages.

    πŸ“Œ Quick Revision Summary

    πŸ”Ή Packages

    • Organize classes
    • Avoid conflicts
    • Use package and import

    πŸ”Ή Exception Handling

    • Handles runtime errors
    • Keywords: try, catch, finally, throw, throws
    • Types: Checked & Unchecked

    ← Previous topic 1
    Java API: Abstract classes and Interfaces
    Next topic 3 β†’
    Advanced issues of GUI and event handling

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    611words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Advanced Computer Programming