English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

argsTest.java
class argsTest {
public static void main(String args[]) {
//args[0] = "null";
//args[0] = "test";
try {
for(int i =0;i System.out.println(args[i]);
}
catch(Exception ex)
{
System.out.println("Exception");
}
catch(NullPointerException npe)
{
System.out.println("NullPointerException");
}
}
}
Output :
D:\>javac argsTest.java
argsTest.java:13: exception java.lang.NullPointerException has already been caug
ht
catch(NullPointerException npe)
^
1 error

2006-08-18 03:08:28 · 3 answers · asked by K Ban 2 in Computers & Internet Programming & Design

3 answers

class argsTest {
public static void main(String args[]) {
//args[0] = "null";
//args[0] = "test";
try {
for(int i =0;i System.out.println(args[i]);
}
catch(NullPointerException npe)
{
System.out.println("NullPointerException");
}
catch(Exception ex)
{
System.out.println("Exception");
}

}
}

because

The class "Exception" must always be used last
which means before u Catch an exception with a Base class ("Exception")u must catch the sub Exception class(NullPointerException)

If u do it otherwise it will always give an error message to u

try
{
}
catch(NullPointerException e)
{
}
catch(Exception e)
{
}
i hope u got the point if not just mail me

2006-08-18 03:25:29 · answer #1 · answered by Achilles 2 · 0 0

NullPointerException is a subclass of Exception class.
To avoid this error, 1st catch the NullPointerException and then catch any generic Exception

2006-08-18 10:43:31 · answer #2 · answered by Neil 5 · 0 0

A Java Exception Handling Example

Class myClass

{

public myClass();

{

}

public void functionA()

{

try

{

// code that could throw an exception

// i.e. open a file

functionB();

if(/* BAD_DATA*/)

{

// int_ExceptionA thrown:

IntException int_ExceptionA = new IntException();

throw int_ExceptionA;

}

}

catch(IntException e)

{

// code that handles an integer exception

// int_ExceptionA caught

}

catch(DoubleException e)

{

// handle double exceptions

// double_ExceptionA caught

// double_ExceptionB is caught

}

finally

{

// close the file

// EOFException caught (by closing the file)

}

}

public void functionB() throws DoubleException, IOException

{

try

{

// code that could throw an exception

functionC();

try

{

// code that could throw an exception

// byte_ExceptionA thrown

}

catch(ByteException e)

{

// byte_ExceptionA caught and rethrown

throw e;

}

// byte_ExceptionB thrown

}

catch(ByteException e)

{

// handle a byte exception

// byte_ExceptionA caught

// byte_ExceptionB caught

}

// double_ExceptionA is thrown

// double_ExceptionB is propagated due to lack of handler

// EOFException thrown syntax: "throw new EOFException();"

// EOFException is propagated due to lack of handler

}

public void functionC() throws DoubleException

{

// double_Exception2 is thrown

}


public static void main(String args[])

{

functionA();

}

}


Commentary:

There are two types of exceptions:

Checked: those handled by the compiler, and therefore, not a concern of the programmer. Checked exceptions are derived from the Error class.

Unchecked: those that the programmer must handle. Unchecked exceptions inherit from the RuntimeException class. Often, unchecked exceptions result from errors in source code that the programmer should not have allowed anyhow.

Exception objects are given as the operand ("thrown") to the throw statement as follows:

throw ExceptionObject;

or

throw new ExceptionObject();

from within a try block:

try {

...

}

Functions can declare that they may throw an exception using the header:

ReturnType functionName(parameters) throws ExceptionType


A function may declare that it throws more than one exception in its header.


If a function declares that it throws an exception that is an instance of a particular class with one or more derived classes, it may also throw an exception of one of those derived classes (as in EOFException of functionB())


The finally clause signifies actions to be taken regardless of whether an exception has been thrown or not, and regardless of whether an exception has been caught or not. finally clauses are used for tasks such as closing a file. finally blocks must be placed below all catch statements, at the end of the try-catch block and have the syntax:

finally{

...

}


If an exception is thrown inside a try block, it is bound to the first handler (catch) immediately following the try clause whose parameter matches the same class as the exception thrown. If an exception is not caught within the construct it is thrown, it is propagated back in the control stack to the construct that called the code that threw the exception.

Note that if a statement in a try block throws an exception, execution of the try block is halted and an appropriate handler is searched for beginning with the method on the top of the call stack. After the exception handler has been executed, control passes to the finally block, if one exists. Following execution of the finally block, the program continues execution at the first line following the finally block. If an exception is thrown by a try block and not handled by a catch clause immediately following the try block, the finally clause is executed before the exception is propagated. If no handlers can be found for an exception, the exception is propagated back to the main() and the program terminates.

An exception that is caught may be rethrown via a throw statement without an operand at the end of the handler. Furthermore, a throw statement of one exception handler may, itself, throw an exception other than the one it is designed to handle. In the words of Cay Horstman, "As a general rule, you should catch those exceptions that you know how to handle and propagate those that you do not know how to handle" (Horstman, Core Java 1.2 Volume 1).

Additionally, since software often enters conditions not adequately managed by built-in exception types, it is also possible to create new exception classes that extend class Exception or inherit from a child class of class Exception.


For more information, try:

Sun's Java Tutorial:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/exception.html

http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html

http://java.sun.com/docs/books/tutorial/essential/exceptions/putItTogether.html

Information sources include Core Java 1.2 Volume 1 (Horstman, Cornell, 1999), Concepts of Programming Languages Fourth Edition (Sebesta, 1999), and the Java tutorial at http://www.java.sun.com

2006-08-18 10:25:47 · answer #3 · answered by Anonymous · 0 0

fedest.com, questions and answers