Command line arguments - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Command line arguments

Description:

to return a value from main use System.exit(int) exit stops the program even if other ... public void open_file() throws IOException. Generating exceptions ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 15
Provided by: robertb61
Category:

less

Transcript and Presenter's Notes

Title: Command line arguments


1
Command line arguments
  • Argument to main
  • array of strings
  • args that appear after the class name

2
Program exit value
  • main function returns void
  • to return a value from main use System.exit(int)
  • exit stops the program even if other threads are
    running

3
environment
  • java cannot read environment variables
  • java can read system properties
  • String xSystem.getProperty(user.home)
  • the system sets some properties
  • you may set properties using the -D option
  • java -Dmyapp.debugtrue myapp

4
name space
  • no global variables or functions
  • every variable or function is a member of a class
  • every class is a member of a package
  • the fully qualified name of a member includes the
    package name, class name and member name

5
import statement
  • makes classes available under an abbreviated name
  • import package.class
  • import package.

6
access
  • all classes and interfaces in a package are
    accessible to all other classes and interfaces in
    the same package
  • a class declared public is accessible in other
    packages
  • members of a class are accessible from other
    class in the same package if not declared
    private.

7
access
  • a member of class A is accessible from a class B
    in a different package if A is public and the
    member is public, or if A is public, the member
    is protected and B is a subclass of A

8
Exceptions
  • a signal that indicates an exceptional condition,
    such as an error, has occurred
  • to throw an exception is to signal an exceptional
    condition
  • to catch an exception is to handle it.

9
exceptions
  • if an exception is not caught by the block of
    code that throws it, it propagates up to the next
    level
  • if never caught it causes java to print an error
    message and terminate

10
exception object
  • an exception is an object
  • subclass of java.lang.Throwable
  • different exception objects have different data
    describing the exception
  • Throwable includes an error message that is
    inherited by all exceptions

11
try/catch/finally
  • try establishes a block of code to have its
    exceptions handled
  • followed by zero or more catch clauses that
    handle specified types of exception
  • followed by a finally clause that always gets
    executed

12
exceptions
trysomething catch(Exception e)do something
about an exception finallydo this even if no
exception occurred
13
declaring exceptions
  • any method that can cause an exception must
    either catch the exception or specify the type of
    exception with a throws clause
  • public void open_file() throws IOException

14
Generating exceptions
  • throw new MyException(my exception occurred)
  • class MyException extends Exception
  • public MyException(String s)super(s)
Write a Comment
User Comments (0)
About PowerShow.com