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:
Error: This is a bug that the program cannot handle
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