public static void main (String [] args) {} |
|
/**
This program shows the arguments of the function main, that is the sring elements
of the array named args. Executing the java class needs just to set the values of
the fuction as arguments. This program holds also an example of rising exceptions.
*/
// First try: ----------------------------------
/*
class Arguments {
public static void main (String [] args)
{
//try{
for (int i = 0 ; i<=3; i++){
System.out.println("\n\t args (" + i+ ") = " + args [i]);
}
//}
//catch (Exception e){
System.out.println("\n\t The exception is catched.");// The message is: " + e.getMessage());
//}
}
}
C:\Java>javac Arguments.java
C:\Java>>java Arguments
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Arguments.main(Arguments.java:7)
C:\Java>
*/
// Second try: ------------------------------------------------------
class Arguments {
public static void main (String [] args)
{
try{
for (int i = 0 ; i<=3; i++){
System.out.println("\n\t args (" + i+ ") = " + args [i]);
}
}
catch (Exception e){
System.out.println("\n\t The exception is catched. The message is: " + e.getMessage());
}
}
}
// Execution: -----------------------------------------------
/*
C:\Java>javac Arguments.java
C:\Java>java Arguments
The exception is catched. The message is: 0
C:\Java>java Arguments 0
args (0) = 0
The exception is catched. The message is: 1
C:\Java>java Arguments 1 2
args (0) = 1
args (1) = 2
The exception is catched. The message is: 2
C:\Java>java Arguments 2 44 567
args (0) = 2
args (1) = 44
args (2) = 567
The exception is catched. The message is: 3
C:\Java>java Arguments 2 44 567 777
args (0) = 2
args (1) = 44
args (2) = 567
args (3) = 777
C:\Java>java Arguments 1 2 3 4 5
args (0) = 1
args (1) = 2
args (2) = 3
args (3) = 4
C:\Java>java Arguments 1 2 3 4 5 6
args (0) = 1
args (1) = 2
args (2) = 3
args (3) = 4
C:\Java>
*/
// ------------------------------------------------------------------
public static void main (String [] args) {
System.out.println("The argument of the main method is an array of String elements.");}
© The Scientific Sentence 2007. |