JAVA Exception Handling

Source: Internet
Author: User

In the past, when I learned C language, I generally used if to handle exceptions;

When the Score Management System is written once and a user is required to input an integer,

If you enter a character, the entire program crashes,

This situation is beyond the if force, and exception handling can solve such problems well;

In JAVA:


[Java]
Import java. util .*;
 
Public class TestInput {
Public static void main (String [] args ){
Pipeline SC = new pipeline (System. in );
Try {
Int I = SC. nextInt ();
System. out. println (I );
}
Catch (Exception e ){
System. out. println ("input error! ");
E. printStackTrace (); // enter exception information. e is an object.
}
}
}

Import java. util .*;

Public class TestInput {
Public static void main (String [] args ){
Pipeline SC = new pipeline (System. in );
Try {
Int I = SC. nextInt ();
System. out. println (I );
}
Catch (Exception e ){
System. out. println ("input error! ");
E. printStackTrace (); // enter exception information. e is an object.
}
}
}

 


[Java]
 


At ordinary times, there are many fish exceptions: NULL pointers, subscripts out of bounds, and arithmetic exceptions;

 

 

An exception is also a class object, in which Error is a serious Error and cannot be handled by programmers;

Exception is a must-handle Exception. RuntimeException is a non-handled Exception. The parent classes of these Exception classes are Throwable;

Exception Handling steps:

Throw try catch finally throws

Throws an exception and captures it;

Let's look at an example:


[Java]
Import java. io .*;
Class
{
Public void f ()
{
Throw new IOException (); // IO exception is a must-handle exception;
}
}
Public class TestInput
{
Public static void main (String [] args)
{
A aa = new ();
}
}

Import java. io .*;
Class
{
Public void f ()
{
Throw new IOException (); // IO exception is a must-handle exception;
}
}
Public class TestInput
{
Public static void main (String [] args)
{
A aa = new ();
}
}
This code will report an error during compilation because only the exception that must be handled is thrown but not handled,

Method 1:


[Java]
Import java. io .*;
Class
{
Public void f () throws IOException
{
Throw new IOException (); // IO exception is a must-handle exception;
}
}
Public class TestInput
{
Public static void main (String [] args)
{
A aa = new ();
}
}

Import java. io .*;
Class
{
Public void f () throws IOException
{
Throw new IOException (); // IO exception is a must-handle exception;
}
}
Public class TestInput
{
Public static void main (String [] args)
{
A aa = new ();
}
} Indicates that an exception is thrown but not handled by this function. The exception is handled by the called function;

Method 2:


[Java]
Import java. io .*;
Class
{
Public void f () throws IOException
{
Try
{
Throw new IOException ();
} Catch (Exception e)
{

}

}
}
Public class TestInput
{
Public static void main (String [] args)
{
A aa = new ();
}
}

Import java. io .*;
Class
{
Public void f () throws IOException
{
Try
{
Throw new IOException ();
} Catch (Exception e)
{

}

}
}
Public class TestInput
{
Public static void main (String [] args)
{
A aa = new ();
}
}
This method is handled by yourself in this function;

Exception Handling Mechanism:

When an exception occurs, the compiler first searches for the exception solution in this function. If the exception solution is not found in this function, the compiler jumps to the called function to find the solution, finally, jump to the main function. If the main function is still not found, it will be handed over to the Java Virtual Machine for processing. The processing of the virtual machine is to terminate the program directly and print the error on the screen, this is the exception handling mechanism;

Custom exception:

Custom Exception classes must inherit the Exception class (recommended) instead of the Throwable class;

The Exception cannot be processed because the Throwable class subclass has an Error;

Custom exception classes are rarely used;

Note that the exception scope thrown by the subclass is smaller than that of the parent class, and the exception of the subclass must be solved before the exception is solved;

 

 

 

 

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.