ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Advanced Computer Programming
    COMP3114
    Progress0 / 12 topics
    Topics
    1. Java API: Abstract classes and Interfaces2. Packages and Exception handling3. Advanced issues of GUI and event handling4. Applets and Swing5. Network Programming Concepts: JDBC6. Multithreading7. Building Client/Server and implementing protocols8. RMI (Remote Method Invocation)9. Java Secure Socket Extension and Secure Sockets Layer (SSL)10. SSL Socket and SSL Server Socket classes11. Client and Server Authentication: HTTPS12. Developing TCP/IP client and server with telnet
    COMP3114›Packages and Exception handling
    Advanced Computer ProgrammingTopic 2 of 12

    Packages and Exception handling

    4 minread
    611words
    Beginnerlevel

    📘 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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time4 min
      Word count611
      Code examples0
      DifficultyBeginner