It depends. The IRS lets you withdraw money from your 401(k) while still employed for certain types of financial hardships. To qualify, the withdrawal must be made on account of an "immediate and heavy financial need" and must be necessary to satisfy that need.
Assuming you meet those requirements and your employer’s plan permits hardship withdrawals, you can take out money to buy a primary residence, prevent foreclosure or eviction from your home, repair certain damages to your home, pay college tuition due in the next 12 months for yourself or a dependent, pay certain unreimbursed medical expenses for you or your dependents or pay funeral expenses.
You will owe tax on your withdrawal (regardless of age) and if you are younger than 59.5, a 10 percent penalty. You may be able to borrow money from your plan to purchase your first home without a penalty.
For more on hardship distributions, see www.irs.gov/retirement/article/0,,id=162416,00.html or http://www.guideto401khardships.com/