473,372 Members | 1,059 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

how to rethrown an exception in java?

2
how to rethrown an exception in java?
Oct 1 '07 #1
8 10917
dmjpro
2,476 2GB
how to rethrown an exception in java?
Post your code and let me know what you tried :-)

Kind regards,
Dmjpro.
Oct 1 '07 #2
Nayna
2
I want to rethrown an exception but I do not know how to rethrown exception so please help me.
Oct 1 '07 #3
r035198x
13,262 8TB
I want to rethrown an exception but I do not know how to rethrown exception so please help me.
Do you know how to catch an exception in first place?
Oct 1 '07 #4
u can use throw new { whatever exception u are going to throw }
Oct 1 '07 #5
JosAH
11,448 Expert 8TB
u can use throw new { whatever exception u are going to throw }
You don't even have to create a new Exception for that; have a look:

Expand|Select|Wrap|Line Numbers
  1. try {
  2.    // code here that throws an Exception
  3. }
  4. catch (Throwable t) {
  5.    throw t; // (re)throw it in their face
  6. }
  7.  
kind regards,

Jos
Oct 1 '07 #6
r035198x
13,262 8TB
You don't even have to create a new Exception for that; have a look:

Expand|Select|Wrap|Line Numbers
  1. try {
  2.    // code here that throws an Exception
  3. }
  4. catch (Throwable t) {
  5.    throw t; // (re)throw it in their face
  6. }
  7.  
kind regards,

Jos
Spoil sport.
Oct 1 '07 #7
JosAH
11,448 Expert 8TB
Spoil sport.
So sorry; may I lick the legs of your chair? I'll rip my head off now and nail it to the floor;
later I'll rethrow it in the sceptic tank. thank you very much for your merci Sir ;-)

kind regards,

Jos
Oct 1 '07 #8
Sometimes youíll want to rethrow the exception that you just caught, particularly when you use Exception to catch any exception. Since you already have the handle to the current exception, you can simply re-throw that handle:
Expand|Select|Wrap|Line Numbers
  1. catch(Exception e) {
  2.   System.out.println("An exception was thrown");
  3.   throw e;
  4. }
  5.  
Rethrowing an exception causes the exception to go to the exception handlers in the next-higher context. Any further catch clauses for the same try block are still ignored. In addition, everything about the exception object is preserved, so the handler at the higher context that catches the specific exception type can extract all the information from that object.
If you simply re-throw the current exception, the information that you print about that exception in printStackTrace( ) will pertain to the exceptionís origin, not the place where you re-throw it. If you want to install new stack trace information, you can do so by calling fillInStackTrace( ), which returns an exception object that it creates by stuffing the current stack information into the old exception object. Hereís what it looks like:

Expand|Select|Wrap|Line Numbers
  1. //: Rethrowing.java
  2. // Demonstrating fillInStackTrace()
  3.  
  4. public class Rethrowing {
  5.   public static void f() throws Exception {
  6.     System.out.println(
  7.       "originating the exception in f()");
  8.     throw new Exception("thrown from f()");
  9.   }
  10.   public static void g() throws Throwable {
  11.     try {
  12.       f();
  13.     } catch(Exception e) {
  14.       System.out.println(
  15.         "Inside g(), e.printStackTrace()");
  16.       e.printStackTrace();
  17.       throw e; // 17
  18.       // throw e.fillInStackTrace(); // 18
  19.     }
  20.   }
  21.   public static void
  22.   main(String[] args) throws Throwable {
  23.     try {
  24.       g();
  25.     } catch(Exception e) {
  26.       System.out.println(
  27.         "Caught in main, e.printStackTrace()");
  28.       e.printStackTrace();
  29.     }
  30.   }
  31. } ///:~ 
  32. The important line numbers are marked inside of comments. With line 17 un-commented (as shown), the output is:
  33. originating the exception in f()
  34. Inside g(), e.printStackTrace()
  35. java.lang.Exception: thrown from f()
  36.         at Rethrowing.f(Rethrowing.java:8)
  37.         at Rethrowing.g(Rethrowing.java:12)
  38.         at Rethrowing.main(Rethrowing.java:24)
  39. Caught in main, e.printStackTrace()
  40. java.lang.Exception: thrown from f()
  41.         at Rethrowing.f(Rethrowing.java:8)
  42.         at Rethrowing.g(Rethrowing.java:12)
  43.         at Rethrowing.main(Rethrowing.java:24) 
  44. So the exception stack trace always remembers its true point of origin, no matter how many times it gets rethrown.
  45. With line 17 commented and line 18 un-commented, fillInStackTrace( ) is used instead, and the result is:
  46. originating the exception in f()
  47. Inside g(), e.printStackTrace()
  48. java.lang.Exception: thrown from f()
  49.         at Rethrowing.f(Rethrowing.java:8)
  50.         at Rethrowing.g(Rethrowing.java:12)
  51.         at Rethrowing.main(Rethrowing.java:24)
  52. Caught in main, e.printStackTrace()
  53. java.lang.Exception: thrown from f()
  54.         at Rethrowing.g(Rethrowing.java:18)
  55.         at Rethrowing.main(Rethrowing.java:24) 
  56.  
Because of fillInStackTrace( ), line 18 becomes the new point of origin of the exception.
The class Throwable must appear in the exception specification for g( ) and main( ) because fillInStackTrace( ) produces a handle to a Throwable object. Since Throwable is a base class of Exception, itís possible to get an object thatís a Throwable but not an Exception, so the handler for Exception in main( ) might miss it. To make sure everything is in order, the compiler forces an exception specification for Throwable. For example, the exception in the following program is not caught in
Expand|Select|Wrap|Line Numbers
  1. main( ):
  2. //: ThrowOut.java
  3. public class ThrowOut {
  4.   public static void
  5.   main(String[] args) throws Throwable {
  6.     try {
  7.       throw new Throwable(); 
  8.     } catch(Exception e) {
  9.       System.out.println("Caught in main()");
  10.     }
  11.   }
  12. } ///:~
  13.  
Itís also possible to rethrow a different exception from the one you caught. If you do this, you get a similar effect as when you use fillInStackTrace( ): the information about the original site of the exception is lost, and what youíre left with is the information pertaining to the new throw:
Expand|Select|Wrap|Line Numbers
  1. //: RethrowNew.java
  2. // Rethrow a different object from the one that
  3. // was caught
  4.  
  5. public class RethrowNew {
  6.   public static void f() throws Exception {
  7.     System.out.println(
  8.       "originating the exception in f()");
  9.     throw new Exception("thrown from f()");
  10.   }
  11.   public static void main(String[] args) {
  12.     try {
  13.       f();
  14.     } catch(Exception e) {
  15.       System.out.println(
  16.         "Caught in main, e.printStackTrace()");
  17.       e.printStackTrace();
  18.       throw new NullPointerException("from main");
  19.     }
  20.   }
  21. } ///:~ 
  22.  
The output is:

originating the exception in f()
Caught in main, e.printStackTrace()
java.lang.Exception: thrown from f()
at RethrowNew.f(RethrowNew.java:8)
at RethrowNew.main(RethrowNew.java:13)
java.lang.NullPointerException: from main
at RethrowNew.main(RethrowNew.java:18)


The final exception knows only that it came from main( ), and not from f( ). Note that Throwable isnít necessary in any of the exception specifications.

You never have to worry about cleaning up the previous exception, or any exceptions for that matter. Theyíre all heap-based objects created with new, so the garbage collector automatically cleans them all up.
Nov 15 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: ChInKPoInt [No MCSD] | last post by:
I am using Visual Studio 2K3 writing a ASP.NET web application. Is there a way to force the C# compiler to catch possible exception? In Java, all exceptions thrown MUST BE caught, otherwise...
4
by: Vivek | last post by:
Hi, I have a question regarding ThreadAbortException. Why is the thread abort exception rethrown at the end of a catch clause? Why is ThreadAbortException's behavior designed to be this way? ...
4
by: D'artagnan | last post by:
I'm using Pthreads in a C++ program that uses exceptions. When one thread is cancelled (cancellation state is deferred) by the main thread, it throws some mysterious exception which cannot even be...
2
by: james | last post by:
I have a console app with the code: public static void Main(String args) { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler( delegate(object sender,...
2
by: bevis | last post by:
I'm new to sql server and mysql but this seems like it should be a pretty straight forward jdbc connection. But I have spent almost 2 days just trying to get a jdbc connection. Please help if you...
0
by: star111792 | last post by:
helo all i am working with JSP and EJBs. i have written a simple code for verifying username and password of user by using session beans. my problem is that i am getting the following exception: ...
0
by: fergallydon | last post by:
Hi, I'm trying to implement some centralised exception handling and logging in the Application_Error() event of global.asax. If the code that generates the exception is not inside a try-catch...
0
by: neuraljay | last post by:
hi everyone. I am trying to build a jsp using webwork, hibernate and mysql. And right now I am doing its unit testing. Everytime I run the test I am getting this error: Error JDBC exception on...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.