'JAVA/JSP/Java'에 해당되는 글 13건
- 2008.07.14 Exception/ Sort
--------------- Exception ---------------
* Exception(예외처리)
- 자바는 프로그램 실행중 발생할 수 있는 예외를 처리할수 있는 기법을 제공.
발생하는 모든 예외를 객체로 취급, 예외관련 클래스를 제공.
.예외의 종류)
- 정수를 0으로 나누는 경우
- 배열 첨자를 음수, 배열크기 벗어나는 참조를 했을경우
- 부적절한 형변환
- 입출력
try
{
예외가 발생할만한 코드;
}
catch(예외타입 변수명)
{
예외 발생시 처리할 코드;
}
try
{
1
2
3
}
catch( ){
}
catch( ) {
}
catch( ) {
}
finally{ } // 예외와 상관없이 실행해야하는 문장
---------------------------------------------------------------------------
import java.io.*;
public class Exceptest
{
public static void main(String[] args)
{
int i;
try
{
FileReader file = new FileReader("c:/java/a.txt");
while((i=file.read()) != -1)
{
System.out.print((char)i);
}
file.close();
}
catch(Exception e)
{
System.out.println(" Exception :"+e);
// e.toString();
// e.printStackTrace(); (후에 많이 쓰임)
}
}
}
--------------- Exception 상속관계 ---------------
import java.io.*;
public class Exceptest
{
public static void main(String[] args)
{
int i;
try
{
FileReader file = new FileReader("c:/java/a.txt");
while((i=file.read()) != -1)
{
System.out.print((char)i);
}
file.close();
}
catch(FileNotFoundException e)
// IOException의 자식 클래스이므로 앞에 써주어야 한다.
{
System.out.println("FileException :"+e);
}
catch(IOException e)
{
System.out.println("IOException :"+e);
}
}
}
------------------------------------------------------------------
public class Exceptest2
{
public static void main(String[] args)
{
try
{
System.out.print("number :");
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
System.out.println("a = "+a+" b = "+b);
System.out.println("a/b = "+(a/b));
System.out.println("alright!!");
}
catch(ArithmeticException e) // 0으로 나누었을때
{
System.out.println("Arithmetic Exception>>>>>>>>>>"+e);
}
catch(NumberFormatException e) // 숫자문자가 아닌 문자열을 입력했을때
{
System.out.println("NumberFormat Exception>>>>>>>>>"+e);
}
catch(ArrayIndexOutOfBoundsException e) // 문자열 한개만 입력했을때
{
System.out.println("ArrayIndexOutOfBounds Exception>>>>>>>>"+e);
}
}//main
}// class
------------- finally ------------
public class Exceptest2
{
public static void main(String[] args)
{
try
{
System.out.print("number :");
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
System.out.println("a = "+a+" b = "+b);
System.out.println("a/b = "+(a/b));
System.out.println("alright!!");
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception>>>>>>>>>>"+e);
}
catch(NumberFormatException e)
{
System.out.println("NumberFormat Exception>>>>>>>>>"+e);
return;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception>>>>>>>>"+e);
}
finally
{
System.out.println("<<<Ending Exception and running finally bluck>>>");
}
System.out.println("<<<< The End >>>");
}//main
}// class
------------------Sort -------------------
import java.io.*;
public class Sort
{
public static void main(String[] args) throws IOException
{
int[] arr = new int[5];
int temp;
System.out.println("******* insert number *********");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(int a=0; a<5; a++)
{
System.out.print("insert number"+a+" :");
arr[a]= Integer.parseInt(br.readLine());
}
//////////// 정렬알고리즘 /////////////
for(int i=0; i<arr.length-1; i++)
{
for(int j=i+1; j<arr.length; j++)
{
if(arr[j]>arr[i])
{
temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;
}
}
}
/////////////////////////////////////
for(int k=0; k<arr.length; k++)
System.out.println(arr[k]);
}
}
'JAVA/JSP > Java' 카테고리의 다른 글
DB Connection (0) | 2008.10.06 |
---|---|
MouseEvent/ItemEvent (0) | 2008.07.14 |
instanceof/StringTokenizer/innerclass (0) | 2008.07.14 |
read()/ readLine()/ Package (0) | 2008.07.14 |
Choice/Checkbox/List/TextArea //Event (0) | 2008.07.14 |