Use of Java exception classes

Source: Internet
Author: User

1. Concept of anomalies

What is an exception? The program error is divided into two parts, and the compile time is coarse and the runtime error occurs. Compile-time error is the compiler when compiling the source code errors occur;

A run-time error is an error that occurs at run time when the compilation passes. This condition is called an exception.

For example: array out of bounds, divisor 0, file not found and so on.

The level of the exception:

Object

Throwable

Error

Exception

RuntimeException

IOException

2. How to resolve exceptions

A. Using Try...catch

  

 Public class Usingtrycatch {    publicstaticvoid  main (String args[]) {          int a = 5;         int b = 0;         int result = 0;         Try {            = A/ b;         Catch (Exception e) {            System.out.println (e);     }}}

B. Using the throws or throw keyword

 Public classUsingthrows { Public Static voidMain (String args[]) {Try{caculation caculation=Newcaculation (); } Catch(Exception e) {e.printstacktrace (); }    }     Public Static classcaculation{intA = 5; intb = 0; intresult = 0;  PublicCaculation ()throwsexception{Result= A/b;        SYSTEM.OUT.PRINTLN (result); }    }}

 Public classUsingthrow { Public Static voidMain (String args[]) {Try {//caculation caculation = new Caculation ();        Throw NewException ("instanced Exception"); } Catch(Exception e) {e.printstacktrace (); }}     Public Static classcaculation{intA = 5; intb = 0; intresult = 0;  PublicCaculation ()throwsexception{Result= A/b;        SYSTEM.OUT.PRINTLN (result); }    }}

Use of Java exception classes

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.