Talk about Java again

Catch exceptions
If an exception is not caught anywhere, the program will terminate and print the exception information on the console. This includes the type of exception and the contents of the stack. For a graphical interface, also printed information in the stack after exception is caught, but the program will return the processing cycle of the user interface (GUI when debugging a program, it is best to ensure that the console window is visible and not minimized)
in order to catch an exception must Set try/catchstatement

try =
{
	code
	more code
	more code
}
catch(ExceptionType e)
{
	handler for this type
}

If an exception class described in the catch clause is thrown in any code in the try statement block, then
1. The program will skip the remaining code in the try statement block
2. The program will execute the processor code
in the catch clause if If no error is thrown in the try statement, then the catch code block will be skipped. If an exception is thrown in try. Did not think of a suitable catch code block, then the method will directly exit.
There are two ways to handle exceptions.
One is the try / catch statement to catch the exception through the catch statement when the exception
is thrown. The other is to directly throw the exception and pass the exception to the caller.
When choosing these two methods, usually, you should catch those exceptions that know how to handle, and those that don't know how to handle will continue to be passed. If you want to pass an exception, you must add a throws specifier at the beginning of the method . If you write a method that overrides the superclass, and this method does not throw an exception, then this method must catch every checked exception in the method code. It is not allowed to appear in the throws specifier of the subclass beyond the scope of the exception class listed in the superclass method.
Catch multiple exceptions
Catch multiple exception types in a try statement block and make different treatments for different types. E.g:

try
{
	code that might throw exception
}
catch(FileNotFoundException e)
{
	emergency action for missing files
}
catch(UnknowHostException e)
{
	emergency action for unknow hosts
}
catch(IOException e)
{
	emergency action forall other I/O problems
}

An exception object may contain information related to the exception class. You can use it to e.getMessage()obtain detailed
error information or use e.getClass().getName()the actual type of the exception object.
In Java SE 7, the same catch sub-statement can capture multiple exception types, for example:

try
{
	code that might throw exception
}
catch(FileNotFoundException |UnknowHostException e)
{
	emergency action for missing files andunknow hosts
}
catch(IOException e)
{
	emergency action forall other I/O problems
}

When catching an exception, the exception variable is implicitly a final variable.
Exceptions and exception chains
are thrown again. An exception can be thrown in the catch clause. The purpose of this is to change the exception type. If a subsystem is developed for use by other programmers , Then the anomaly class that represents a subsystem failure may produce multiple explanations.

try
{
	access the	database
}
catch(SQLException e)
{
	throw new ServletException("database error"+e.getMessage);
}

Set the original exception as the "cause" of the new exception

try
{
	access the database
}
catch(SQLException e)
{
	Throwble se = new ServletException("database error");
	se.initCause;
	throw se;
}

When an exception is caught, you can use the following statement to
retrieve the original exception: Throwble e = se.getCause; It is
strongly recommended to use this packaging technique. This allows users to throw high-level exceptions in the subsystem without losing the details of the original exception.
The compiler will look at the throw statement in the catch block, and then look at the type of e, and will point out that this method can throw any Exception instead of SQLException. This problem has now been improved. The compiler will track that e comes from the try block.
Finally clause:
When the code throws an exception, it will terminate the processing of the remaining code in the method and exit the execution of this method. But if you want to release the local resources of the method, there is a scheme that is more tedious, catching and rethrowing all exceptions, but you need to clear the resources in two places: one is in the normal code, and the other is in the exception code.
There is a better solution in Java, using finally, whether or not an exception occurs, finally will be executed.
example:

InputStream in = new FileInputStream(...);
try
{
	//1
	code that might throw exception
	//2
}
catch (IOException e)
{
	//3
	show error message
	//4
}
finally
{
	//5
	in.close
}
//6

In the above code, the following three cases will be executed finally.
1. The code does not throw an exception. The program first executes all the code in try, then executes the code in the finally clause, and then executes the first statement after try, executes 1, 2, 5, and 6.
2. If the try statement throws an exception, then the execution of the code under try will be terminated, and then the catch will be executed, and finally finally. If the catch does not throw an exception, then the first statement after try will be executed.
1, 2, 4, 5, 6.
3. The code throws an exception, but the catch does not catch the exception, it will execute finally, and will not execute the first statement of the
try statement , 1, 5. The try statement may only be finally, and the
return statement will return without catch finally, But suppose there is also a return in the try statement, and finally will also be executed, so that the value returned by return in finally will overwrite the value in try.
Sometimes the finally clause will also cause trouble, and the method of cleaning up resources may throw an exception.

InputStream in=...;
try
{
	code that might throw exceptions
}
finally
{
	in.close();
}

An exception may be thrown in try, but the same type of exception in try may also be thrown in finally to overwrite the previous exception. You can use the following methods

InputStream in=...;
Exception ex=null;
try
{
	try{
		code that might throw exceptions
		}
	catch(Exception e)
	{
		ex=e;
		throw e;
	}
}
finally
{
	try
	{
		in.close();
	}
	catch(Exception e)
	{
		if(ex==null)throw e;
	}
}

The simplest form of a try statement with resources is

try(Resource res=...)
{
	work with res
}

javaSE 7 provides this code with a void close() throws Exceptiontry statement block that automatically calls res.close () when it exits.
You can call the getSuppressed method to get the exception thrown by the close method.
The try statement with resources can also have a catch clause and a finally clause, these clauses will be executed after closing the resource.
Analyze the stack trace The
stack trace is a list of method invocation procedures. It contains the specific location of the method invocation during program execution. You can call the printstackTrace method of the Throwable class to access the text description information of the stack trace.

Published 74 original articles · won 15 · views 4094

Guess you like

Origin blog.csdn.net/V2636011127/article/details/105309501