JAVA throwsとthrowの違い

842 ワード

//学習ノートとしてのみ
/*

throws  throw   
1,throws      。
2,throw      。

throws            ,     
throw         。

*/



class FuShuException extends Exception
{
	private int value;
	FuShuException(String msg,int value)
	{
		super(msg);
		this.value = value;
	}

	public int getValue()
	{
		return value;
	}
}



class Demo
{
	int div(int a,int b)throws FuShuException//throws      
	{
		if(b < 0)
		{
			throw new FuShuException("           !",b);//throws      
		}
		return a/b;
	}
}

class ExceptionDemo3 
{
	public static void main(String[] args) 
	{

		Demo d = new Demo();


		try
		{
			int x = d.div(4,-1);							
			System.out.println(" x = "+ x);
		}
		catch (FuShuException e)
		{
			System.out.println(e.toString());
			System.out.println("       :"+e.getValue());
		}

	}
}