Created
January 1, 2025 07:23
-
-
Save Visnusah/5c5835466521d1e9ae4e3d2680f86695 to your computer and use it in GitHub Desktop.
TeamWarica
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HelloWorld { | |
public static void main(String[] args) { | |
System.out.println("Hello, World!"); | |
} | |
} |
Exception Handling In Java
public class ExceptionHandling{
public static void main(String[] args) {
System.out.println("Start of program");
// Attempting to divide by zero to demonstrate ArithmeticException
try {
int num1 = 100/0; // This line will throw ArithmeticException
System.out.println("Value of num1: "+num1);
} catch (ArithmeticException e) {
System.out.println("ArithmeticException caught: " + e);
}
// try - catch - finally block to handle ArithmeticException
try{
int num2 = 100/0; // This line will throw ArithmeticException
// If an exception occurs, the program will jump to the catch block
// If an exception occurs, the following line will not be executed
System.out.println("Value of num2: "+num2);
}catch(ArithmeticException ex){
// Catching ArithmeticException and printing the exception message
System.out.println("ArithmeticException caught: " + ex);
}
// Array exception handling
try
{
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[10]); // This line will throw ArrayIndexOutOfBoundsException
}
catch(ArrayIndexOutOfBoundsException ex)
{
// Catching ArrayIndexOutOfBoundsException and printing the exception message
System.out.println("ArrayIndexOutOfBoundsException caught: " + ex);
}
// NULL pointer exception handling
String name = null; // Setting a string to null to demonstrate NullPointerException
try {
{
System.out.println(name.length()); // This line will throw NullPointerException
}
} catch (NullPointerException e) {
// Catching NullPointerException and printing the exception message
System.out.println("NullPointerException caught: " + e);
}
// common Catch block to catch any type of exception
try
{
int num2 = 100/0; // This line will throw ArithmeticException
}
catch (Exception e)
{
// Catching any type of exception and printing the exception message
System.out.println("Common Exception caught: " + e);
}
finally
{
// This block will always execute, regardless of whether an exception occurred or not
System.out.println("Finally block executed");
}
// Multiple catch blocks to handle different types of exceptions
// Attempting to access an array index that is out of bounds to demonstrate exception handling
try{
int[] numbers2 = {1, 2, 3, 4, 5};
System.out.println(numbers2[10]); // This line will throw ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException error) {
// Catching ArrayIndexOutOfBoundsException and printing the exception message
System.out.println("ArrayIndexOutOfBoundsException caught: " + error);
} catch(ArithmeticException error) {
// Catching ArithmeticException and printing the exception message
System.out.println("ArithmeticException caught: " + error);
} catch (Exception error) {
// Catching any other type of exception and printing the exception message
System.out.println("Common Exception caught: " + error);
}
System.out.println("End of program");
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Python Library Examples: Pandas, Numpy, Matplotlib, and Seaborn
This script demonstrates how to use popular Python libraries for data manipulation, numerical computation, and data visualization. It covers
pandas
,numpy
,matplotlib
, andseaborn
in a single file.Script Overview
The script includes examples of:
Full Code Example
Example for numpy
Example for matplotlib
Example for seaborn
Installation
Libraries Used
pandas: For creating and manipulating data.
numpy: For numerical computations.
matplotlib: For creating static, interactive, and animated visualizations.
seaborn: For statistical data visualization.
tabulate: For formatting and displaying tabular data.
Purpose
This script provides a quick overview of working with Python libraries commonly used in data science and analytics. It is suitable for beginners and can be extended for advanced use cases.
Output
Pandas DataFrame: Displays a tabular dataset.
Numpy Array: Prints a numerical array.
Matplotlib Plot: Displays a simple line graph.
Seaborn Plot: Shows a styled statistical plot with custom configurations.