Eclipse and Exception handling

Source: Internet
Author: User
Tags arithmetic stub

shortcut keys: Ctrl+d Delete the line where the cursor is located

alt+/Smart Tips

Exception handling

An exception is an issue that prevents the current method or scope from continuing to execute, and some instructions in the program that cause the program to break.

Try and Catch keywords

try{

Code snippet with possible exception

}catch (Exception type Object) {

Handling Operations for exceptions

}...

finally{

Anomaly of the unified export

}

Cases:

1  Public classPractice14 {2     3      Public Static voidMain (string[] args) {4         //TODO auto-generated Method Stub5         Try{6         intnum1=10;7         intNum2=0;8System.out.println (num1/num2);9}Catch(ArithmeticException e) {TenSystem.out.println ("Arithmetic Operation exception"); One         } A         finally{ -             //finally: You can do some recycling cleanup here -SYSTEM.OUT.PRINTLN ("Finally statement will execute whether or not an exception occurs.")); the         } -     } -      -}

Throw and throws keywords:

The throws keyword is primarily used on the declaration of a method, which means that the exception is not handled in the method and is left to the call processing.

The Throw keyword indicates that an exception is thrown manually in the program, because the exception from the processing mechanism, once all exceptions are generated, is actually thrown by an instantiated object of an exception class, then this object can also be thrown directly from the throw.

1  Public classPractice14 {2     3      Public Static voidMain (string[] args) {4         //TODO auto-generated Method Stub5         //exTest ();6         Try {7             intResult=extest2 (10,2);8 System.out.println (result);9}Catch(Exception e) {Ten             //TODO auto-generated Catch block One e.printstacktrace (); A         } -     } -     Private Static intExTest2 (intNUM1,intNUM2)throwsexception{ the         if(num2==0)Throw NewArithmeticException ("Divisor cannot be 0"); -         returnnum1/num2; -     } -     Private Static voidexTest () { +         Try{ -         intnum1=10; +         intNum2=0; ASystem.out.println (num1/num2); at}Catch(ArithmeticException e) { -System.out.println ("Arithmetic Operation exception"); -         } -         finally{ -             //finally: You can do some recycling cleanup here -SYSTEM.OUT.PRINTLN ("Finally statement will execute whether or not an exception occurs.")); in         } -     } to      +}

Eclipse and Exception handling

Related Article

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.