The PrinterException 
class and its subclasses are used to indicate that an exceptional condition has occurred in the print system. This Graphics2D 
class extends the Graphics class to provide more sophisticated control over geometry, coordinate transformations, color management, and text layout. This is the fundamental class for rendering 2-dimensional shapes, text and images on the Java(tm) platform. 

解决方案 »

  1.   

    throws PrinterException语句负责抛出一个关于打印的异常错误也就是你的代码可能会出现一些未知的情况导致你无法打印,通过这种捕获异常来保护代码Graphics2D g2 = (Graphics2D)g  通过将参数g转化为Graphics2D类型来创建一个新的Graphics2D实例g2
      

  2.   

    throws PrinterException把PrinterException异常向上抛出,抛给了这个方法的调用者,就是说public int print(Graphics g, PageFormat pf, int page)这个方法会引发一个PrinterException异常,而你在这个方法体中没有捕捉它(try catch),就是没有对它处理,Java为了代码的安全和稳定,要求编程者必须对异常做出处理,所以如果你不在方法体中处理,就必须显示的抛出(用throws语句),同样在此方法调用者中,如果你仍然不处理,则必须继续向上抛出此异常,直到你处理了为止,如果你一直没有处理最终,异常将会被抛到main(即程序入口)方法中,当然你还可以把它抛出,那么它就由JVM处理了:)。
    一般处理异常有两种不同的思路:第一个是就近处理,哪抛出异常,就在那处理。
    第二个是集中处理,就是把异常都向上抛出,在适当的地方用try加多个catch,集中处理。