KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com4j > ComException


1 package com4j;
2
3 /**
4  * Signals a failure in the COM method invocation.
5  *
6  * <p>
7  * Calling a wrapped COM method throws this exception
8  * when the underlying COM method returns a failure HRESULT code.
9  *
10  * @author Kohsuke Kawaguchi (kk@kohsuke.org)
11  */

12 public class ComException extends RuntimeException JavaDoc {
13     private final int hresult;
14
15     private final String JavaDoc fileName;
16     private final int line;
17
18     public ComException( String JavaDoc msg, int hresult, String JavaDoc fileName, int line ) {
19         super(Integer.toHexString(hresult)+' '+cutEOL(msg));
20         this.hresult = hresult;
21         this.fileName = fileName;
22         this.line = line;
23     }
24
25     public ComException( String JavaDoc msg, String JavaDoc fileName, int line ) {
26         super(msg);
27         this.hresult = -1;
28         this.fileName = fileName;
29         this.line = line;
30     }
31
32
33     /**
34      * Gets the HRESULT code of this error.`
35      */

36     public int getHRESULT() {
37         return hresult;
38     }
39
40     private static String JavaDoc cutEOL( String JavaDoc s ) {
41         if(s==null)
42             return "(Unknown error)";
43         if(s.endsWith("\r\n"))
44             return s.substring(0,s.length()-2);
45         else
46             return s;
47     }
48
49     public String JavaDoc toString() {
50         String JavaDoc s = super.toString();
51         if(fileName!=null) {
52             s += ' '+fileName+':'+line;
53         }
54         return s;
55     }
56 }
57
Popular Tags