day81

improt java.awt.*;

import java.awt.event.*;

import java.awt.image.*;

import java.io.*;

import java.util.logging.*;

import javax.swing.*;

public class LoggingImageViewer{

pubic static void main(String[] args){

 if (System.getProperty("java.util.logging.config.class")==null && System.getProperty("java.util.logging.config.file")==null){

try{

Logger.getLogger("").setLevel(Level.ALL);

final int LOG_ROTATION_COUNT=10;

Handler handler=new FIleHandler("%h/LoggingImageViewer.log"),0,LOG_ROTATION_COUNT);

LOgger.getLOgger("").addHandler(handler);

}

catch (IOException e){

Logger.getLogger("comhorstmann.corejava").log("level.SEVERE," Can't create log file handler,e);

Handler windowHandler=new WindowHandler();

windowHandler.setLevel(Level.All);

Logger.getLoggr("com.horstmann.corejava").addHandler(windowHandler);

JFrame frmae=new ImageViewerFrame();

frame.setTitle("LOgging ImageVIewer");

frame.setSize(300,400);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

LOgger.getLogger("com.horstmann.corejava").fine("Showing frame");

frame.setVisible(true);

}

}

class ImageViewerFrame extends JFrame{

public ImageViewerFrame(){

logger.enering("ImageViewerFrame","<init>");

JMenuBar menuBar=new JmenuBar();

setJMenuBar(menuBar);

JMenu menu=new JMenu("FIle");

JMEnuITem openItem=new JMenuItem("open");

menu.add(openItem);

openIem.addActionLIstener(new FileOpenLIstener());

JMenuItem exitItem=new JmenuItem("exit");

menu.add(exitItem);

exitItem.addActionLIstener(new ActionLIstener(){

 public void actionPerformed(ActionEvent event){

logger.fine("Exiting.");

System.exit(0);

}

});

label=new JLabel();

add(label1);

logger.exiting("ImageViewerFrame","<init>");

}

private class FileOPenLIstener implements AcionLIstener{

public void actionPerformed(ActionEvent event){

logger.entering("ImageviewerFrame.FileOpenLIstener","actionPerformed",event);

JFileChooser chooser=new JFileChooser();

chooser.setCurrentDirectory(new File(","));

chooser.setFileFilter(new javax.swing.filechooser.FIleFilter(){

public boolean accept(File f){

 return f.getName().toLowerCase().endWith(".gif")||f.isDirctory();

}

public String getDescription(){

return "GIF Images";

}

});

int r=chssoer.soowOpenDialog("ImageViewerFrame.this");

if (r==JFileChooser.APPROVE_OPTION){

String name=chooser.getSelectedFile().getPah();

logger.log(Level.Fine,"Reading file{0}",name);

label.setIcon(new ImageIcon(name));

}

else

logger.fine("File open dialog canceled.");

logger.exiting("ImageviewerFrame.FileOpenLIstener","actionPerformed");

}

}

privagte JLabel label;

priavet static Logger logger=logger.getLogger("com.horstman.corejava");


}

}

}

RuntimeException运行时异常类
ArithmeticException算术异常类
ArrayStoreException数组存储异常类
ClassCastException类型强制转换异常类
IndexOutOfBoundsException下标越界异常类
NullPointerException空指针异常类
SecurityException违背安全原则异常类

  

p Exception的其它子类
n EmptyStackException
n NoSuchFieldException
n NoSuchMethodException
n ClassNotFoundException
n CloneNotSupportedException
n IllegalAccessException
n InstantiationException
n InterruptedException
p Java输入输出的异常类
n IOException:申请I/O操作没有正常完成。
n EOFException:在输入操作正常结束前遇见了文件结束符。
n FileNotFountException:在文件系统中,没有找到由文件名字符串指定的文件。
p

p 从Exception或IOException 派生一个子类即可。
p  Class FileException extends
                                         IOException
    {
        public FileException() {}
        public FileException(String gripe)
        {    }
    }
String ReadData(in) throw FileExcetion
{
 …
while(…)  {  if(ch==-1)
      {   if(n<len)
                 throw new FileException();
     }
   …}
   …}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值