Exceptions in Java-oriented objects

Source: Internet
Author: User
Tags throw exception throwable

Exceptions and Errors in Java
Exception mechanism in Java to better enhance program robustness

Throwable for top, Error and Exception

Error: Virtual machine errors, memory overflow, thread deadlock

Exception:runtimeexception is null pointer exception, array subscript out of bounds exception, arithmetic exception, type conversion exception, IO exception (ioexception), SQL Exception (SQLException).

Exception handling, exception handling in Java: Throwing exceptions and catching exceptions

Description of the exception:

Class exceptiondemo{
public static void Main (string[] args) {
int[] arr = new INT[3];
System.out.println (Arr[0]);
Result is 0
System.out.println (Arr[3]);
The result is Figure 1
}
}

System.out.println (Arr[3]); Compile no problem, the syntax is not a problem, compile the memory is not an array, run time only in the heap memory to open up the array space. ARR[3] does not have this subscript, so the result cannot be found at run time.

Figure 1, the array subscript out-of-bounds exception, SYSTEM.OUT.PRINTLN (Arr[3]), the runtime has an exception of arrayindexoutofboundexception, causing the program to run, the program is terminated, not executing.

Description of the error

Class exceptiondemo{
public static void Main (string[] args) {
int[] arr = new int[10241024x7681024];
System.out.println (Arr[0]);
Result is 0
System.out.println (Arr[3]);
The result is Figure 1
}
}

Figure 2, which indicates a run-time error and a heap memory overflow.

Differences between exceptions and errors
For the exception is by the solution, Java provides the corresponding processing mechanism, and the error is not, there is no way to solve the targeted, the only way is to error, modify the code.

The process of the exception
In exceptional cases, the problem that occurs at run time is that the array subscript bounds the exception, the problem thrown in the exception is the name, the content, the location of the occurrence, etc., and a variety of information is encapsulated into the object.

ArrayIndexOutOfBoundsException

public class ArrayIndexOutOfBoundsException extends Indexoutofboundsexception

Construction method

ArrayIndexOutOfBoundsException ()
arrayindexoutofboundsexception (int index)
ArrayIndexOutOfBoundsException (String s);

ArrayIndexOutOfBoundsException can be a new object, and a new object can be constructed. Create the object and throw it if you encounter a problem, new ArrayIndexOutOfBoundsException (index).

How to throw it? With the keyword throw, an exception occurs, in the Java virtual machine, the JVM needs to throw the problem, to the caller main, the main function received the exception object thrown, but the main function is not handled, continue to throw the caller JVM, the JVM received an exception after the problem, the exception information displayed on the screen.

Exception Overview (unexpected, exception)
What is an exception? Common:

Try-catch-finally
Try-catch-finally
public void Method () {
try {
Code snippet
Code snippet that produces an exception
}catch (Exception type ex) {
Code snippet to process the exception
}finally{
Code snippet
}
}

> throw> throwsthrows 声明时要进行抛出的异常throw 要手动将产生的异常抛出> public void method() throws Exception1,Exception2,...,ExceptionN {>     // 产生异常代码> }

throw new IOException ();
Self-throwing problems themselves for exception resolution.
The exception type is indicated by the throws keyword at the point where the exception is thrown
public void Method () throws exception type {
Code throws
Throw new Exception type ();
}

Custom exceptions
Exception chain

Exception handling is categorized as:

Throw exception
Catching exceptions
Simple case

public class test{
public static void Main (string[] args) {
try{
String msg = Redtext ("C:\a.txt");
}catch (Pathnotexistexception e) {
For processing
}
}
}

Throwable exception Top-level parent class
There are two ways to handle exception Exception, one for capturing, and two for continuing to throw out compile-time exceptions.

RuntimeException run-time exception, only at run time will appear, can handle, also can not handle.

Custom exceptions, you can define your own exceptions, you define a class, if the class inherits an exception class, inherits a Exception or other exception, that is, defines a compile-time exception, if the inheritance is run-time exception runtimeexception or its subclasses, defines a Run-time exception.

The Throwable class is a superclass of all errors or exceptions in Java, and can be thrown through a virtual machine or a throw statement in Java only if the object is an instance of the class.

Exception is divided into two major categories
Non-check exception (unchecked Exception): compiler does not require forced handling of exceptions
Check for exceptions (Checked Exception): compiler requires exceptions that must be handled, such as IO exceptions
Catching exceptions

Try
Catch
Finally

Declaring exceptions, throwing exceptions

Throws
Throw

If a method has an exception, but is not capable of processing, you can use throws at the method to declare the throw exception, who calls this method, who will deal with the exception. ‘

public void Method () throws Exception1,exception2,..., Exceptionn {
The code for the exception
}

Exception Handling in Java
JAVA exceptions
How to use the > try...catch...finally structure

Class test{
public static void Main (String args[]) {

try{
int i = 1/0;
}
catch (Exception e) {
E.printstacktrace ();
}
finally{
System.out.println ("finally");
}
SYSTEM.OUT.PRINTLN (5);
}
}

Class test{
public static void Main (String args[]) {
try{
Thread.Sleep (1000);
}
catch (Exception e) {
E.printstacktrace ();
}
}
}

The effects of throw and throws differ:

Class person{
private int age;

public void Setage (int.) throws exception{
if (age<0) {
RuntimeException e = new RuntimeException ("age cannot be less than 0");
Throw e;
}
This.age = age;
}
}

Class test{
public static void Main (String args[]) {
person person = new person ();
try{
Person.setage (-1);
}
catch (Exception e) {
System.out.println (e);
}
}
}

The difference between error and exception
Error is a subclass of Throwable that is used to mark critical errors
Exception is a subclass of Throwable that instructs reasonable programs to want to go catch condition, non-critical error
Try/catch the execution of the process
If an exception occurs, the system throws an exception,

(Catch operation), or at the last (finally) to be processed.

The difference between throw and throws
Throws appears on the method declaration, and the throw appears in the method body.

Exception classification
Exception classification: Can be checked for exceptions, run-time exceptions and errors

Exception chain
The exception chain throws another exception after we catch an exception

Three, one-to-one

Exceptions in Java-oriented objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.