Skip to content

This is a tutorial project. JavaBasics_Task_173_V0.1 Demonstrating manual exception throwing using throw new IllegalArgumentException for game score validation. 250226_1119

License

Notifications You must be signed in to change notification settings

YuriiJavaDev/JavaBasics_Task_173_V0.1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Game Score Validator (JavaBasics_Task_173_V0.1)

📖 Description

This project demonstrates how to manually throw exceptions in Java using the throw keyword. It ensures that game scores remain positive by triggering an IllegalArgumentException when an invalid value is processed.

📋 Requirements Compliance

  • Task: Implement displayPositiveScore(int currentScore) with input validation.
  • Validation: Throw IllegalArgumentException if currentScore is 0 or negative.
  • Message: "Cannot display negative score! The number is negative."
  • Mechanism: Handle the exception in main using try-catch.
  • Goal: Maintain data integrity and provide clear feedback.

🚀 Architectural Stack

  • Java 8+

🏗️ Implementation Details

If the condition currentScore < 0 is met, the program explicitly throws a new IllegalArgumentException. The main method catches the error and prints the exception's message to the console.

📋 Expected result

Current score: 50
Error: Cannot display negative score! The number is negative.

💻 Code Example

Project Structure:

src/com/yurii/pavlenko/
                └── Solution.java

Code

package com.yurii.pavlenko;

public class Solution {

    public static void displayPositiveScore(int currentScore) {
        if (currentScore < 0) {
            throw new IllegalArgumentException("Cannot display negative score! The number is negative.");
        }
        System.out.println("Current score: " + currentScore);
    }

    public static void main(String[] args) {
        displayPositiveScore(50);

        try {
            displayPositiveScore(-5);
        } catch (IllegalArgumentException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

⚖️ License

This project is licensed under the MIT License.

Copyright (c) 2026 Yurii Pavlenko

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...

License: MIT

About

This is a tutorial project. JavaBasics_Task_173_V0.1 Demonstrating manual exception throwing using throw new IllegalArgumentException for game score validation. 250226_1119

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages