The difference between throws and throws for java Exception Handling

Source: Internet
Author: User
Tags try catch

The difference between throws and throws for java Exception Handling

1. Differences

Throws is used to declare all the exception information that a method may throw. throws declares but does not handle the exception. Instead, it uploads the exception and sends it to anyone who calls the exception. Throw refers to a specific exception type thrown.

2. Introduction

Throws: Used to declare exceptions. For example, if a method does not want to handle any exceptions, you must declare all possible exceptions in this method (in fact, if you don't want to handle them yourself, just give them to others and tell others what exceptions will occur and report your own errors, let others handle it ).

Format: Method Name (parameter) throws exception class 1, exception class 2 ,.....

1 class Math {2 public int div (int I, int j) throws Exception {3 int t = I/j; 4 return t; 5} 6} 7 8 public class ThrowsDemo {9 public static void main (String args []) throws Exception {10 Math m = new Math (); 11 System. out. println ("Start Operation:" + m. div (10, 2); 12} 13}

 

Throw: handle exceptions by yourself. You can either catch exceptions by yourself (that is, try catch ), either declare to throw an exception (that is, throws exception ~~).

Note:

Once throw is executed, the program will be immediately transferred to the exception handling stage, and subsequent statements will not be executed, and the method will no longer return meaningful values!

1 public class TestThrow 2 {3 public static void main (String [] args) 4 {5 try 6 {7 // call the method with throws declaration, the Exception must be explicitly caught. // otherwise, it must be declared again in the main method to throw 9 throwChecked (-3); 10} 11 catch (Exception e) 12 {13 System. out. println (e. getMessage (); 14} 15 // call the method that throws a Runtime exception. You can either explicitly capture the exception, or ignore the exception 17 throwRuntime (3 ); 18} 19 public static void throwChecked (int a) throws Exception20 {21 if (a> 0) 22 {23 // automatically throw Exception 24 // The code must be in the try block or 25 throw new Exception in the method with throws Declaration ("the value of a is greater than 0, does not meet the requirements "); 26} 27} 28 public static void throwRuntime (int a) 29 {30 if (a> 0) 31 {32 // throw a RuntimeException exception, you can either explicitly capture this exception 33 // or ignore it completely, and send this exception to the method caller to handle 34 throw new RuntimeException ("the value of a is greater than 0, does not meet the requirements "); 35} 36} 37}

 

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.