Java Learning notes----exceptions

Source: Internet
Author: User

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/77/6D/wKiom1Znw8PRRqFRAAF1yjNXwCA449.jpg "title=" 1354020417_5176.jpg "alt=" Wkiom1znw8prrqfraaf1yjnxwca449.jpg "/>

This is a picture stolen from another website.

Exceptions are divided into two types:

    1. Error: This is a bug that the program cannot handle

    2. Exception: This is an exception and is divided into two types:



Methods for handling Exceptions:

Throw exception:

Throws for example, the car is faulty, the car can not be handled by themselves, it is only to people to deal with

The throw appears inside the function body, which is a custom exception class. You can export your own defined exceptions.

Note: This throw also needs throws myexception. At first I thought I didn't have to. So it went wrong.

Code:

package com.ehealth.exc;import java.lang.exception;public class exctest {static  Int question (int x,int y)  throws myexception{if (y<0) {throw new  MyException ("Divisor cannot be negative");} return x/y;} Static int que (int x,int y )  throws myexception{if (x<0) {throw new  myexception ("The first number cannot be less than 0");} Return x+y;} Public static void main (String[] args)  {// todo auto-generated method  stubint x=3;int y=-1;int i=-1;int j=1;try {int a=que (I,J);}  catch  (MYEXCEPTION&NBSP;E1)  {// TODO Auto-generated catch  BlockSystem.out.println (E1.getmessage ());} Try{int res=question (x, y);} catch (myexception e) {System.out.println (E.getmessage ());} catch (arithmeticexception e) {System.out.println (E.getmessage ());} catch (exception e) {System.out.println ("The program has other exceptions!") ");}}} Class myexception&nbSp;extends exception{string message;public myexception (String err) {MESSAGE=ERR;} Public string getmessage () {return message;}}

Catch exception: Try{}catch (Exception e) {}

Originating From: http://blog.csdn.net/hguisu/article/details/6155636

This article from "ehealth" blog, declined reprint!

Java Learning notes----exceptions

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.